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 07-26-2011, 08:39 AM   #1
LQ Newbie
Registered: Jul 2011
Posts: 2

Rep: Reputation: Disabled
sed substitution not working

I new to scripting and thought that I would automate a couple things. I read up on sed and I am confused about it and regular expressions. I am trying to replace some text on one line in a file with some text.
original text: $normal[''];
replaced with: $normal['localhost'];
In my script file file I've tried numerous combinations of delimiters, etc but obviously don't understand all the rules of regular expressions. My current sed command was:
sed -i 's/\$normal[''];/\$normal['localhost'];/g' /path_to_file
I also saw some examples using variables, so I tried that:
echo $bb1
echo $bb2
sed -i 's/"$bb1"/"$bb2"/g' /path_to_file
In the above test, the echos both show the correct text but nothing happens to the real file.

Thanks for any help.
Old 07-26-2011, 09:18 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391

The main problem is not the regular expression, but the fact that the sed command uses single quotes for its commands ('s/.../.../'). You can replace the single quotes with double quotes to get rid of that problem ("s/.../.../"). You also need to escape the square brackets in the search part.

Try this:
sed -i "s/\$normal\[''\];/\$normal['localhost'];/" file
Hope this helps.
1 members found this post helpful.
Old 07-26-2011, 09:41 AM   #3
LQ Newbie
Registered: Jul 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi drunna,
I entered your command and got a s unterminated error, so I added the /g at the end and it worked great!

I could have sworn I tried that sequence but it may not have been the same since I was getting frustrated.

Thank you
Old 07-26-2011, 10:18 AM   #4
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
I still like to always use single quotes for regular expressions, and use these when I want to insert a single quote or bash variable:

'this is a single quote: '\'', this is a variable: '"$var"', and this is more text'


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] Sed substitution using & linuxScriptGirl Linux - Newbie 9 03-17-2011 10:20 AM
SED - substitution carolflb Linux - Newbie 5 02-06-2010 12:20 AM
variable substitution in sed gaynut Programming 1 07-14-2008 07:38 AM
simple substitution with sed? ocicat Programming 9 02-22-2008 11:45 PM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 12:36 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:04 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration