LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   replacing text in file with sed. Parenthesis giving me trouble. (http://www.linuxquestions.org/questions/linux-server-73/replacing-text-in-file-with-sed-parenthesis-giving-me-trouble-4175490456/)

awreneau 01-07-2014 02:32 PM

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) ];

syg00 01-07-2014 06:19 PM

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.

Smokey_justme 01-07-2014 06:42 PM

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)..

sycamorex 01-07-2014 06:56 PM

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.

awreneau 01-08-2014 07:01 AM

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.

awreneau 01-08-2014 07:45 AM

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.

sycamorex 01-08-2014 12:26 PM

Quote:

Originally Posted by awreneau (Post 5094250)
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


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