Operating System - HP-UX
1834292 Members
3485 Online
110066 Solutions
New Discussion

script or sed/awk command to grep pattern and append line after it ?

 
SOLVED
Go to solution
Sammy_2
Super Advisor

script or sed/awk command to grep pattern and append line after it ?

How do I
1) grep for a pattern in a line beginning with (for instance : DS)
2) Comment out that line (for instance : #DS)
3) and append another line right after after it (for instance: DSabc.domain.com

Basically modify /etc/mail/sendmail.cf file using a one line sed command or some script:
See below

==========================

###DS
DSmyname.domain.com
======================

Thanks
good judgement comes from experience and experience comes from bad judgement.
7 REPLIES 7
Rodney Hills
Honored Contributor

Re: script or sed/awk command to grep pattern and append line after it ?

perl -pi -e 'if (s/^DS/#DS/) { print $_}; $_="DSabc.domain.com\n"' yourfile

HTH

-- Rod Hills
There be dragons...
Hein van den Heuvel
Honored Contributor
Solution

Re: script or sed/awk command to grep pattern and append line after it ?

working variation:
$cat x
DS
xDS
DSx
$
$perl -pi -e 'if (/^(DS)/) { print "#$1\n"; chop; $_ .="abc.domain.com\n"}' x
$
$cat x
#DS
DSabc.domain.com
xDS
#DS
DSxabc.domain.com

In this example one liner you only have to specify you select string once and it remembers and use it (in $1 and $_)
The data example includes a 'false' match to make you think about how you want to handle those cases.

To latch onto just the search string on a line use: /^(DS)$/

Hein.


Sammy_2
Super Advisor

Re: script or sed/awk command to grep pattern and append line after it ?

Rodney,
Thanks. But now my file only has many lines with just:

DSabc.domain.com
DSabc.domain.com
DSabc.domain.com
...
..
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: script or sed/awk command to grep pattern and append line after it ?

Hein,
You may have what I am looking for as I did a quick test. I will give it a shot tom. as I have to leave. But Thanks a bunch. I will post results tom.
good judgement comes from experience and experience comes from bad judgement.
Mel Burslan
Honored Contributor

Re: script or sed/awk command to grep pattern and append line after it ?

I am under the impression you are trying to comment out smart relay directives out of your sendmail.cf file.

Here is how I do it without perl

# sed -e "1,\$s/^DS/#DS/" /etc/mail/sendmail.cf > /tmp/sm.cf
# cat /tmp/sm.cf > /etc/mail/sendmail.cf

this way, you do not mess up your file permissions for sendmail.cf

________________________________
UNIX because I majored in cryptology...
Sammy_2
Super Advisor

Re: script or sed/awk command to grep pattern and append line after it ?

Mel, Good answer. Got me thinking and made me find utility of sed command. You answer will comment
out the line (what I wanted) but does not append abc.domain.com to
the the end of next line. (See #3 in my question above).
DSabc.domain.com. Thanks.

Hein, Very Very close to what I wanted. Your script commented out just #DS or $1 and ignored the rest of the line. But again,
I admit I did not explain correctly exactly what I wanted otherwise you would have given me the exact answer and thus you deserve full points.
Below is what I used which was just tweaking your command a little bit. Thanks Hein.

perl -pi -e 'if (/^(DS)/) { print "#$_\n"; chop; $_ .="abc.domain.com\n"}' /etc/mail/sendmail.cf
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: script or sed/awk command to grep pattern and append line after it ?

Thanks to Rodney, Hein and Mel.
good judgement comes from experience and experience comes from bad judgement.