user2392984
user2392984

Reputation: 13

Print subset of file from line number to first match of regex using sed

I would like to print the content of a file starting a particular line number till the first occurrence of a pattern and immediately stop the search & print. I tried this one:

sed -n '2,/{p; :loop n; p; /pattern/q; b loop}'

but without success. How can this be achieved? Thanks for your help.

Upvotes: 1

Views: 194

Answers (3)

potong
potong

Reputation: 58558

This might work for you (GNU sed):

sed '2,/pattern/!d;//q' file

Upvotes: 0

Cyrus
Cyrus

Reputation: 88899

With GNU sed: to stop after a line containg pattern:

sed -n '2,/pattern/{p;/pattern/q;} file

Upvotes: 0

anubhava
anubhava

Reputation: 786011

You can use sed:

sed -n '2,/pattern/p' file

Or this awk command:

awk 'NR==2{p=1} p; /pattern/{exit}' file

Upvotes: 1

Related Questions