LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed -characters (https://www.linuxquestions.org/questions/linux-newbie-8/sed-characters-4175412666/)

musiqdefunk 06-21-2012 09:06 AM

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

David the H. 06-21-2012 10:47 AM

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

pixellany 06-21-2012 10:58 AM

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.

musiqdefunk 06-21-2012 04:08 PM

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.

pixellany 06-22-2012 04:49 AM

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

David the H. 06-22-2012 01:35 PM

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/


All times are GMT -5. The time now is 08:34 AM.