LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 01-07-2014, 02:32 PM   #1
awreneau
LQ Newbie
 
Registered: Aug 2003
Location: GA
Distribution: Ubuntu
Posts: 29

Rep: Reputation: 15
replacing text in file with sed. Parenthesis giving me trouble.


I want to replace a line in a file that begins with some text and replace contents within that line. The exact code I'm using is as follows:

Code:
 sed -i s/^\$CFG{systems}/\$CFG{systems}=[ qw(TESTING1 TESTING2) ];/ test.response
I keep getting the error

Code:
-bash: syntax error near unexpected token `('

So I'm not sure how to overcome the parenthesis. I've tried escaping them but to no avail.

The entire line I'm trying to replace is:

Code:
"$CFG{systems}=[ qw(clustnode1) ];"
I want it to read as follows:
Code:
$CFG{systems}=[ qw(node1 node2) ];
 
Old 01-07-2014, 06:19 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,271

Rep: Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028
Lots of issues there:
- as a general principle you should only replace what needs replacing. That regex (if successful) would result in multiple qw's
- use the regex-extended parameter to sed; that way it is less sensitive to special characters like ()
- you will likely run into similar escaping issues with the $ and {} charachters.
 
Old 01-07-2014, 06:42 PM   #3
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 334

Rep: Reputation: 99
Put your expression between ' and ' .. Sed didn't even begin to execute (thus, bash is the one complaining), so right now, it isn't your regex that's faulty (and yes, I think that's faulty too .. escape the paranthesis)..

Last edited by Smokey_justme; 01-07-2014 at 06:44 PM.
 
Old 01-07-2014, 06:56 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,564
Blog Entries: 1

Rep: Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024
Just to give you an idea:

Code:
sed '/systems/s/(.*)/(node1 node2)/' infile
On lines containing the pattern 'systems' it will replace whatever is inside the parentheses (including them) with the pattern '(node1 node2)'. Without more context/information and more of the actual sample lines it's hard to say if it'll 100% work for all the cases.
 
Old 01-08-2014, 07:01 AM   #5
awreneau
LQ Newbie
 
Registered: Aug 2003
Location: GA
Distribution: Ubuntu
Posts: 29

Original Poster
Rep: Reputation: 15
Thank you all very much. I'll use your suggestions and hammer out a solution. I'll reply later w/ my findings. Again, many thanks.
 
Old 01-08-2014, 07:45 AM   #6
awreneau
LQ Newbie
 
Registered: Aug 2003
Location: GA
Distribution: Ubuntu
Posts: 29

Original Poster
Rep: Reputation: 15
OK, sycamorex your command works as expected it does exactly what I want. The only variance is that I want (node1 node2) to be substituted as a variable.

This works for me.


Code:
sed  "/systems/s/(.*)/($sysnames)/" test.response

Rather than just take code and not learning anything. Why did you add the s option following the string sed is to replace and what is the purpose of the (.*)?
Thanks again.
 
Old 01-08-2014, 12:26 PM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,564
Blog Entries: 1

Rep: Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024Reputation: 1024
Quote:
Originally Posted by awreneau View Post
Rather than just take code and not learning anything. Why did you add the s option following the string sed is to replace and what is the purpose of the (.*)?
Thanks again.
Glad it works fine. The first bit:

/systems/ - tells sed to carry out the following operations only on lines containing the pattern 'systems'

's' is a sed command (substitute). A general syntax is:
Code:
s/old_string/new_string/
The '(.*)' literally means: match '(' + zero or more occurrences of any character + ')'
In other words, match (...) and whatever is inside it.

Probably the best tutorial on sed out there is:
http://www.grymoire.com/unix/sed.html
 
  


Reply

Tags
sed


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
replacing text in xml file using sed nano2 Linux - General 3 11-07-2011 06:55 AM
replacing text with an http link with sed? crypted Programming 4 12-10-2010 12:03 PM
Replacing the text in same file using SED command mariakumar Linux - General 9 10-12-2010 12:20 AM
Replacing text in files without using sed dfresh4130 Programming 16 05-28-2009 10:13 AM
Replacing text on specific lines with sed or awk? Lantzvillian Linux - Newbie 5 10-17-2007 09:00 AM


All times are GMT -5. The time now is 11:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration