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 the following sed command :
sed -i '1,/New USB device found/d' <FILE>
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.
This awk one-liner should give what you want:
awk '/New USB device found/{p++}p==1' file
test with your data:
kent$ awk '/New USB device found/{p++}p==1' file
New USB device found
xxx xxx
yyy yyy
zzz zzz
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With