LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-21-2012, 09:06 AM   #1
musiqdefunk
LQ Newbie
 
Registered: Mar 2011
Posts: 22

Rep: Reputation: 0
sed -characters


I've been working with sed for my scripts for a couple of months now and everything seems to be working fine, until now.

What I am simply trying to do is remove ($v/$Z) from this line
O SmtpGreeatingMessage=$j Sendmail $v/$Z; $b in /etc/mail/sendmail.cf

This is my approach to the problem below:

Code:
/bin/sed -i "s/O SmtpGreetingMessage\=\$j Sendmail \$v/\$Z\; \$b/O SmtpGreetingMessage\=\$j Sendmail \; \$b/" /etc/mail/sendmail.cf
 
Old 06-21-2012, 10:47 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I don't see any problem mentioned, other than in a vague "it doesn't work" kind of way.

Please post an actual example of the input text, and what the output should be like. It might also help to show or explain what your previous attempts actually do.

Edit: would something like this do?

Code:
sed -i '/SmtpGreetingMessage/ s| $v/$Z||' /etc/mail/sendmail.cf

Last edited by David the H.; 06-21-2012 at 10:51 AM. Reason: as posted
 
Old 06-21-2012, 10:58 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
several comments:

You don't need to repeat the whole line when doing the replacement---you can use a backreference, or simply use this construct:

Code:
sed -i '/regex1/ s/regex2//' filename
reads: For any line matching regex1, delete the text matching regex2 (and modify the file in place).

Does "=" need to be escaped?

Finally, you very seldom need to match an entire line---look for a regex that is unambiguous in the context of the contents of the file.
 
Old 06-21-2012, 04:08 PM   #4
musiqdefunk
LQ Newbie
 
Registered: Mar 2011
Posts: 22

Original Poster
Rep: Reputation: 0
David the H.

Wow that worked great, why doesn't any of my books mention anything about using the "|" symbol to translate $ and / into simple text, I will be using this format alot more now that I know, again thanks.
 
Old 06-22-2012, 04:49 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
The "|" symbol is not translating anything---it is the delimiter for the sed s command. (In sed, the first character after the s is the delimiter.)
 
Old 06-22-2012, 01:35 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yes, the man page doesn't mention it, but the info page does. Any basic-level ascii character can be used as the delimiter. Most good, detailed tutorials should mention it too. I generally prefer using the "|" pipe myself, but any character that doesn't appear in the expression will do just as well.

Coincidentally, I also posted an expanded explanation in another thread yesterday:

http://www.linuxquestions.org/questi...8/#post4708642


Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt


I also suggest working through a good regex tutorial or two when you have a chance, since tools like sed and grep depend on them so much.

Here are a few regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/
 
  


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
remove particular characters using sed dsids Linux - Software 7 12-14-2010 12:10 AM
[SOLVED] Sed, how do I match even characters only? trist007 Linux - Newbie 3 09-03-2010 07:11 PM
Replacing Characters with sed zokken Programming 9 12-02-2009 07:34 PM
Getting last characters of a line with sed command LULUSNATCH Programming 4 12-21-2005 09:33 AM
using sed to grab two characters? nausicaa3000 Programming 4 04-20-2004 11:01 AM

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

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