LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help getting 'sed' to see [ as a character in a string (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-getting-sed-to-see-%5B-as-a-character-in-a-string-946674/)

localgameworld 05-24-2012 01:52 PM

Need help getting 'sed' to see [ as a character in a string
 
Hello to all.

I am running CentOS 5.8 Linux 32bit.

I have a script that I am trying to run but getting a snag.

The command running inside this script is:

Quote:

sed -e "/FSROTATE_MOD[${DEVICE}]/s\(.*\)/${Fs_Rotate}/\1${FS_ROTATE}/g" < /backup_tar_device_file >/backup_tar_device_file.tmp ;;
The problem here is sed does not see my match string as a string.

It will look for all lines that match FSROTATE_MOD but nothing else.

I need sed to match all lines FSROTATE_MOD[n]

n= any device number, could be 1 could be 4.

I need it to search for the string

FSROTATE_MOD[1] having '[' inside the string.

Any ideas would be helpful.

I already tried

Quote:

sed -e "/FSROTATE_MOD\[${DEVICE}\]/s\(.*\)/${Fs_Rotate}/\1${FS_ROTATE}/g" < /backup_tar_device_file >/backup_tar_device_file.tmp ;;
Putting a '\' infront of the '[' & ']' with no success.

colucix 05-24-2012 01:59 PM

Escaping the square brackets with \ should work. It is the s command that is malformed. Try:
Code:

sed -e "/FSROTATE_MOD\[${DEVICE}\]/s/\(.*\)${Fs_Rotate}/\1${FS_ROTATE}/g"
The / delimiter was displaced after the closing parenthesis, whereas it should be immediately after the s command, before the opening escaped parenthesis.

David the H. 05-24-2012 07:52 PM

Please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

It usually helps to also provide a sample of the input, and the desired output, as well as in this case the contents of the variables.


Your basic problem is that in regex, "[]" represents a range of characters that could match in that position.

Funnily enough, the best course of action is therefore to use brackets around the brackets. You can backslash escape them too, but the brackets are the recommended regex solution and are less likely to interfere with shell syntax, say.

I also recommend using sed's -r option, to avoid needing to backslash escape other important regex characters. Also, if the expressions themselves contain slashes, you can switch to a different delimiting character instead.

BTW, are ${Fs_Rotate} and ${FS_ROTATE} meant to be the same variable, or different? because shell syntax is case sensitive. It might help if you told us what they contain. And is the "g" at the end really necessary? You only need it if there can be multiple replacements on a single line.


Finally, since environment variables are generally all upper-case, it's good practice to keep your own user variables in lower-case or mixed-case to help differentiate them. And please don't use the full bracketed "${var}" form when you don't need it. It just adds clutter.


Code:

sed -r -e "/FSROTATE_MOD[[]$DEVICE[]]/ s/(.*)$Fs_Rotate/\1$FS_ROTATE/g"

Note also that this command will only match a single number (or whatever is in the DEVICE variable. If, as you say, you want every line within a range of 1 to 4, you don't need a variable.

Code:

sed -r -e "/FSROTATE_MOD[[][1-4][]]/ s/(.*)$Fs_Rotate/\1$FS_ROTATE/g"


All times are GMT -5. The time now is 10:02 PM.