I've got some large CSV files where I'd like to extract all data between Line X that includes pattern 'x' and Line Y that includes pattern 'y'
For example:
other_data
Header
data
data
data
Footer
other_data
I want to be able to pipe everything between (and including) Header -> Footer to a new file.
Thanks!
Using awk
it's pretty straightforward:
awk '/Header/ { show=1 } show; /Footer/ { show=0 }'
Basically keep state in a variable named show
. When we hit the Header we turn it on, Footer we turn it off. While it's on, the show
rule executes the default action of printing the record.
Another way with awk
:
awk '/Header/,/Footer/' file
Header
data
data
data
Footer
Just redirect the output to save in a newfile:
awk '/Header/,/Footer/' file > newfile
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