Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 05-24-2012, 01:52 PM   #1
LQ Newbie
Registered: Mar 2011
Posts: 19

Rep: Reputation: 0
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:

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

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.
Old 05-24-2012, 01:59 PM   #2
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Escaping the square brackets with \ should work. It is the s command that is malformed. Try:
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.
Old 05-24-2012, 07:52 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

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.

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

Last edited by David the H.; 05-24-2012 at 07:54 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] search and replace string with special character perl or sed. Noobux Programming 4 05-21-2012 03:16 PM
[SOLVED] sed - prefixing a character string identified by a RegEx danielbmartin Programming 8 01-15-2012 11:17 AM
[SOLVED] SED assistance: add character in the middle of a string. Vryali Programming 4 10-21-2010 10:51 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
Using sed to convert a string to a character? whansard Linux - General 2 01-10-2003 05:13 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration