Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed delete but apend right after

Status
Not open for further replies.

paublo

ISP
Sep 14, 2006
127
US
hi, im trying to figure out how to have sed delete something then append right after.

so lets say i have

sed -e '/.*IN.*MX.*/d' fileA

now on fileA i can have one line that has "IN MX" or many lines but what i would like to do is delete whatever lines have "IN MX" and add/append in the same place 1 new line.

I tried -e 's/.*IN.*MX.*/XXX/ fileA

but this will replace however many instances of IN MX that it finds, so if it finds 4 it will replace IN MX 4 times with XXX and i just want XXX added/append once.

TIA, P.A
 
Try this awk script:

Code:
awk '
        BEGIN { found=0 }
        # replace first IN MX line
        /IN.*MX/ && !found { print "XXX" }
        # skip subsequent IN MX lines
        /IN.*MX/ { found=1; next }
        # print any other line found, and reset flag
        { print; found=0 }
'

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top