Tuesday, 1 October 2013

remove lines after second pattern sed

remove lines after second pattern sed

I'm looking to trim a file and I need to remove everything after the
second match (and everything before the first match).
For example Say I have this text:
xxx xxx
yyy yyy
New USB device found
xxx xxx
yyy yyy
zzz zzz
New USB device found
xxx xxx
yyy yyy
If I use : sed -i '1,/New USB device found/d' this removes everything
before the first match. which is great:
New USB device found
xxx xxx
yyy yyy
zzz zzz
New USB device found
xxx xxx
yyy yyy
But I'm only 1/2 way there, now I want to remove everything after the 2nd
match to get this result:
New USB device found
xxx xxx
yyy yyy
zzz zzz
Hence just the data for the first device. Can anyone help?

No comments:

Post a Comment