LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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
Member
 
Registered: Aug 2003
Location: GA
Distribution: Ubuntu
Posts: 42

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: 21,124

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
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,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
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
Member
 
Registered: Aug 2003
Location: GA
Distribution: Ubuntu
Posts: 42

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
Member
 
Registered: Aug 2003
Location: GA
Distribution: Ubuntu
Posts: 42

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,836
Blog Entries: 1

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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:50 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
Open Source Consulting | Domain Registration