Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed/Awk - pull lines between pattern x and y

Tags:

bash

sed

awk

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!

like image 343
Numpty Avatar asked Apr 02 '13 16:04

Numpty


2 Answers

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.

like image 56
FatalError Avatar answered Oct 07 '22 04:10

FatalError


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
like image 34
Chris Seymour Avatar answered Oct 07 '22 02:10

Chris Seymour