sed or awk delete next line following patter

sed or awk delete next line following patter

am 23.10.2007 18:09:56 von peter sands

Hi,

I have the following formated file:

CONT_UK 10224
pemax
CONT_EC 10221
pemla
CONT_UK 10224
petem
CONT_SE 28000
laik


I need to delete all lines with the pattern containing CONT_UK, I need
also to delete the next line following that pattern. I can delete the
pattern matching lines:
$ sed -e '/CONT_UK 10224/d' contf.txt
pemax
CONT_EC 10221
pemla
petem
CONT_SE 28000
laik


, but how do I delete the next line following that pattern match:

So I end up with:

CONT_EC 10221
pemla
CONT_SE 28000
laik

Thanks for your help
Pete.

Re: sed or awk delete next line following patter

am 23.10.2007 18:24:52 von Cyrus Kriticos

peter sands wrote:
>
> I have the following formated file:
>
> CONT_UK 10224
> pemax
> CONT_EC 10221
> pemla
> CONT_UK 10224
> petem
> CONT_SE 28000
> laik
>
>
> I need to delete all lines with the pattern containing CONT_UK, I need
> also to delete the next line following that pattern. I can delete the
> pattern matching lines:
> $ sed -e '/CONT_UK 10224/d' contf.txt
> pemax
> CONT_EC 10221
> pemla
> petem
> CONT_SE 28000
> laik
>
>
> , but how do I delete the next line following that pattern match:
>
> So I end up with:
>
> CONT_EC 10221
> pemla
> CONT_SE 28000
> laik
>
> Thanks for your help
> Pete.
>

sed -e '/CONT_UK/{N;d}' contf.txt

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.

Re: sed or awk delete next line following patter

am 23.10.2007 18:27:10 von Michael Tosch

peter sands wrote:
> Hi,
>
> I have the following formated file:
>
> CONT_UK 10224
> pemax
> CONT_EC 10221
> pemla
> CONT_UK 10224
> petem
> CONT_SE 28000
> laik
>
>
> I need to delete all lines with the pattern containing CONT_UK, I need
> also to delete the next line following that pattern. I can delete the
> pattern matching lines:
> $ sed -e '/CONT_UK 10224/d' contf.txt
> pemax
> CONT_EC 10221
> pemla
> petem
> CONT_SE 28000
> laik
>
>
> , but how do I delete the next line following that pattern match:
>
> So I end up with:
>
> CONT_EC 10221
> pemla
> CONT_SE 28000
> laik
>
> Thanks for your help
> Pete.
>

Quite easy, you append the next line to the current line then delete:

sed -e '/CONT_UK 10224/{N;d;}' contf.txt

--
Michael Tosch @ hp : com

Re: sed or awk delete next line following patter

am 23.10.2007 18:31:21 von Janis Papanagnou

peter sands wrote:
> Hi,
>
> I have the following formated file:
>
> CONT_UK 10224
> pemax
> CONT_EC 10221
> pemla
> CONT_UK 10224
> petem
> CONT_SE 28000
> laik
>
>
> I need to delete all lines with the pattern containing CONT_UK, I need
> also to delete the next line following that pattern. I can delete the
> pattern matching lines:
> $ sed -e '/CONT_UK 10224/d' contf.txt
> pemax
> CONT_EC 10221
> pemla
> petem
> CONT_SE 28000
> laik
>
>
> , but how do I delete the next line following that pattern match:
>
> So I end up with:
>
> CONT_EC 10221
> pemla
> CONT_SE 28000
> laik
>
> Thanks for your help
> Pete.
>

Try...

sed '/CONT_UK/,+1d'


Janis

Re: sed or awk delete next line following patter

am 24.10.2007 14:53:48 von Ed Morton

peter sands wrote:

> Hi,
>
> I have the following formated file:
>
> CONT_UK 10224
> pemax
> CONT_EC 10221
> pemla
> CONT_UK 10224
> petem
> CONT_SE 28000
> laik
>
>
> I need to delete all lines with the pattern containing CONT_UK, I need
> also to delete the next line following that pattern. I can delete the
> pattern matching lines:
> $ sed -e '/CONT_UK 10224/d' contf.txt
> pemax
> CONT_EC 10221
> pemla
> petem
> CONT_SE 28000
> laik
>
>
> , but how do I delete the next line following that pattern match:
>
> So I end up with:
>
> CONT_EC 10221
> pemla
> CONT_SE 28000
> laik
>
> Thanks for your help
> Pete.
>

awk '/CONT_UK 10224/{c=2}!(c&&c--)' file

Just set "c" to however many lines you want to delete, including the
line where the pattern was found.

Ed.