MaVe
MaVe

Reputation: 1725

Print everything between pattern

I would like to print everything between two two lines that match a certain pattern. For example, if my input file looks like

--- START ---
line 1
line 2
--- END ---

I would like to have as output

line 1
line 2

Can this be done (e.g. using grep or awk?)

Upvotes: 1

Views: 213

Answers (4)

damienfrancois
damienfrancois

Reputation: 59070

you can do

sed -n '/--- START ---/,/--- END ---/{/--- START ---\|--- END ---/!p}'  < input

or

awk '/--- END ---/{exit}; flag {print}; /--- START ---/{flag=1} ' < input

Upvotes: 1

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 184985

Using :

perl -0777 -ne 'print $1 if /^--- START ---\s*\n(.*?)--- END ---/s' file

Upvotes: 0

piokuc
piokuc

Reputation: 26164

Here is a simple solution which uses awk and grep:

awk '/-- START ---/,/--- END ---/ {print $0}' file.txt \
| grep -v -- '--- START ---' \
| grep -v -- '--- END ---'

Upvotes: 0

Jotne
Jotne

Reputation: 41446

This is how to do it with awk

awk '/END/{f=0} f; /START/{f=1}' file.txt
line 1
line 2

You should easily find solution for this using Google.

Another version:

awk '/START/{f=1;next} /END/{f=0} f' file.txt
line 1
line 2

Upvotes: 2

Related Questions