Alex Rivera | Logout

Sed/Awk - pull lines between pattern x and y

Asked 2013-04-02T16:32:58.843
12

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!

Edit
Report

1 Answer

19

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.

answered 2013-04-02T16:35:33.307

Your Answer