LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 08-27-2012, 03:31 PM   #1
careerdad
LQ Newbie
 
Registered: Jul 2006
Location: Atlanta, GA
Distribution: suse 10.1
Posts: 3

Rep: Reputation: 0
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!
 
Old 08-27-2012, 03:33 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Hope this will work

Quote:
sed 's/relayhost\ \=\ \[smtprelay/\#relayhost\ \=\ \[smtprelay/g' test.txt > test.out
 
Old 08-27-2012, 03:48 PM   #3
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
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
 
Old 08-29-2012, 10:49 PM   #4
careerdad
LQ Newbie
 
Registered: Jul 2006
Location: Atlanta, GA
Distribution: suse 10.1
Posts: 3

Original Poster
Rep: Reputation: 0
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!
 
Old 08-30-2012, 03:02 AM   #5
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
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.
 
Old 08-30-2012, 06:13 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
1 members found this post helpful.
Old 08-30-2012, 06:48 AM   #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
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

Last edited by sycamorex; 08-30-2012 at 06:49 AM.
 
Old 08-31-2012, 05:58 AM   #8
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by pixellany View Post
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 View Post
To avoid the need to "escape" the ( ), use sed -r
That's good to know, thanks
 
  


Reply



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] head -n -1 (sed -n -e '1,$-1p' is incorrect) sed question, as stupid as it could be. kaz2100 Programming 1 12-27-2011 09:18 PM
question about sed Magil Programming 13 12-15-2009 05:51 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
question on sed tifu Linux - Newbie 3 03-18-2005 04:38 PM
sed question tifu Programming 5 03-18-2005 12:02 PM

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

All times are GMT -5. The time now is 10:11 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