asddddddaaaad2
asddddddaaaad2

Reputation: 35

extracting between two patterns in sed

Input text file:

#END
this should not be extracted
#BEGIN    
This should be extracted.
#BEGIN
keep going..
These lines should be extracted by our script.

Everything here will be copied.
#END
That should be all.
#BEGIN
Nothing from here.
#END

Desired output:

This should be extracted.
#BEGIN
keep going..
These lines should be extracted by our script.

Everything here will be copied.

My sed script is:

/#BEGIN/,/#END/!d
/#END/q
/#BEGIN/,/#END/{/#BEGIN/d;/#END/d;p;}

My script can do most of what I want it to do except for ignore the first #END and print whatever is in the first #BEGIN #END block (including the nested #BEGIN). I am not sure how to change my script in order to meet these constraints.

Upvotes: 2

Views: 313

Answers (1)

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed -n '/^#BEGIN/,/^#END/!b;//!p;/^#END/q' file

Limit the lines printed to between #BEGIN and #END and only print the non header/footer lines. Quit when hitting the first #END.

N.B. Placing the regexp for quitting following the print command allows the second or more headers to remain in the output.

Upvotes: 1

Related Questions