LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-26-2011, 09:39 AM   #1
flyscubakev
LQ Newbie
 
Registered: Jul 2011
Posts: 2

Rep: Reputation: Disabled
sed substitution not working


Hi,
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:
bb1="\$normal[''];"
bb2="\$normal['localhost'];"
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.
Kevin
 
Old 07-26-2011, 10:18 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Hi,

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:
Code:
sed -i "s/\$normal\[''\];/\$normal['localhost'];/" file
Hope this helps.
 
1 members found this post helpful.
Old 07-26-2011, 10:41 AM   #3
flyscubakev
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, 11:18 AM   #4
MTK358
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:

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


Reply


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 11:20 AM
SED - substitution carolflb Linux - Newbie 5 02-06-2010 01:20 AM
variable substitution in sed gaynut Programming 1 07-14-2008 08:38 AM
simple substitution with sed? ocicat Programming 9 02-23-2008 12:45 AM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 01:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:06 AM.

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