tags:

views:

183

answers:

2

OK I found this question:

http://stackoverflow.com/questions/876446/how-do-i-delete-a-matching-line-the-line-above-and-the-one-below-it-using-sed

and just spent the last hour trying to write something that will match a string and delete the line containing the string and the line beneath it (or a variant - delete 2 lines beneath it).

I feel I'm now typing random strings. Please somebody help me.

+2  A: 

If I've understood that correctly, to delete match line and one line after

/matchstr/{N;d;}

Match line and two lines after

/matchstr/{N;N;d;}
  • N brings in the next line
  • d - deletes the resulting single line
martin clayton
Your command will work without replacing the newlines, just do `/matchstr/{N;d}` or `/matchstr/{N;N;d}`
Dennis Williamson
@Dennis - So it does. I'll make that edit. Thx.
martin clayton
A: 

you can use awk. eg search for the word "two" and skip 2 lines after it

$ cat file
one
two
three
four
five
six
seven
eight
$ awk -vnum=2 '/two/{for(i=0;i<=num;i++)getline}1' file
one
five
six
seven
eight
ghostdog74