LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SED question (https://www.linuxquestions.org/questions/linux-newbie-8/sed-question-4175424286/)

careerdad 08-27-2012 03:31 PM

SED question
 
Hello - I've never posted a real question here before so if this is in the wrong section I apologize. However here's my question:

I want to write a one-liner that goes through a file, finds a search string, replaces with something else and then writes the file. I though sed would be the tool I needed?

I want to search for relayhost = [something and comment that out - here's one of my attempts:

sed 's/relayhost = [smtprelay/#relayhost = [smtprelay/g' <test.txt >test.out

and then I get this:

sed: -e expression #1, char 50: unterminated `s' command

Would someone please help me format this? I've a couple hundred servers I need to make this change on and would rather not manually edit them. :)

Additionally I have to find another line that is commented out and uncomment it.

Thanks for your help in advance!

szboardstretcher 08-27-2012 03:33 PM

Hope this will work

Quote:

sed 's/relayhost\ \=\ \[smtprelay/\#relayhost\ \=\ \[smtprelay/g' test.txt > test.out

SecretCode 08-27-2012 03:48 PM

szboardstretcher's solution looks OK - basically you need to escape the regex special characters. But I think only [ matters. Plus you can use avoid some typing with a capture group:

Code:

sed 's/\(relayhost = \[smtprelay\)/#\1/g' test.txt > test.out

careerdad 08-29-2012 10:49 PM

Both worked!
 
Sorry I took so long to get back to you - I just switched from days to mid shift.

Thank you both - I tested each string individually and they both worked equally well, although SecretCode's way baffled me a bit - I don't understand the group concept but will research.

Thanks again!

SecretCode 08-30-2012 03:02 AM

Groups in regular expressions are very useful. If you surround a section in the left hand side with parentheses ( ) then when you put \1 in the right hand side it gets replaced with whatever was matched in that section of the left hand side.

Say you wanted to comment out any line that had "= [" in it, not just that text, you can't retype the text on the rhs because you don't know what it is:
Code:

sed 's/\(.* = \[.*\)/#\1/g'
It's a bit hard to read because sed requires that you escape the ( ) with the \ character. I don't know why that is - some other commands that support regexes don't need to escape them.

pixellany 08-30-2012 06:13 AM

I'm not sure how universal that construct is....In sed, it's called a "backreference" and it's mostly used in the "s" command.

To avoid the need to "escape" the ( ), use sed -r

sycamorex 08-30-2012 06:48 AM

Not tested (I'm on lunch at work):

Code:

sed '/relayhost = \[smtprelay/s//#&/' file
or

Code:

sed '/relayhost = \[smtprelay/s/^/#/' file
If 'relayhost' is a unique variable, it'll get even simpler:

Code:

sed '/^relayhost/s/^/#/' file

SecretCode 08-31-2012 05:58 AM

Quote:

Originally Posted by pixellany (Post 4768190)
I'm not sure how universal that construct is....In sed, it's called a "backreference" and it's mostly used in the "s" command.

It's universal, although there are some syntax variations.
Regex Tutorial - Named Capturing Groups - Backreference Names:
Quote:

All modern regular expression engines support capturing groups, which are numbered from left to right, starting with one. The numbers can then be used in backreferences to match the same text again in the regular expression, or to use part of the regex match for further processing.
perlrequick - perldoc.perl.org
In perl, the backreferences are \g1 etc (not \1) and there are "matching variables" $1 etc.

Quote:

Originally Posted by pixellany (Post 4768190)
To avoid the need to "escape" the ( ), use sed -r

That's good to know, thanks


All times are GMT -5. The time now is 05:55 AM.