PDA

View Full Version : Find pattern in a file and erase it


c0mrade
06-24-2008, 09:22 PM
Hello,

I'd like to find pattern in a file and erase line containing that pattern and 2 lines below that pattern. I know how to find pattern with grep -r pattern and erase line containing that pattern, but can't figure out the rest, how to erase 2 lines below that pattern line. Any suggestions?

Thank you in advance

lvlamb
06-24-2008, 10:11 PM
sed -n '/pattern/{N;N;d;}' input.file :)

In love with one-liners.:cool:

c0mrade
06-24-2008, 10:40 PM
Thank you for your answer : But the file stays unchanged

sed -n '/ptrisn.com/{N;N;d;}' named.conf

I've google something out but still can't get it working awk '{if ($0 ~/pattern/) {getline; getline; continue}
else print }' old.txt > new.txt I get this error from awk "awk: (FILENAME=old.txt FNR=126) fatal: `continue' outside a loop is not allowed"
anymore hints ?

ephemera
06-25-2008, 12:40 AM
sed -i.bak '/ptrisn.com/{N;N;d;}' named.conf

or

perl -i.bak -pe 'if(/ptrisn.com/){<>;<>;$_=undef}' named.conf

btw, lvlamb's sed command is pretty neat. :)

c0mrade
06-25-2008, 01:24 AM
ephemera you're the king as always you give correct answers to my questions, your the best. tnx

lvlamb
06-25-2008, 02:17 AM
re: file remains unchanged. :eek:
Never heard of directing the input to anoter file,
or modify in place?

I deleted 4 letters in my post, unfortunately they seem to be needed.
RTFM

ephemera
06-25-2008, 06:19 AM
lvlamb, the reason it wasn't working is because of the '-d' option which isn't required in this case.

lvlamb
06-25-2008, 02:36 PM
the -n flag, not -d ;)

Right. I tested with the print function {...;p;} and did not re-re-checked with the delete {...;d;)
You win ;)

Note that, RTFM should have told why.

Anyway, I learned the use of braces.

ephemera
06-25-2008, 06:31 PM
You win ;)
Hmm, i can see both of you are making fun of me :D