1

Given: sed -e '/pattern1/,/pattern2/!d' file.org

How can I match the first occurrence of the lines between pattern1 and pattern2, but not the rest?

For example:

pattern1
aaaa
pattern2
pattern1
bbb
pattern

should output: aaa

Alternatives solutions (using grep, awk or whatever) are welcomed.

Quora Feans
  • 3,866

1 Answers1

1
$ cat input
a
b
c
a
b
c
$ sed -n '/a/,/c/p;/c/q' input
a
b
c

Searches for the range to print, and then quits after seeing the first 'end' marker.

awk makes it a little easier to exclude the start and end points:

$ awk 'BEGIN { p=0 }  /c/ { p=0; exit } p {print} /a/ { p=1 }' input
b
DopeGhoti
  • 76,081
  • I want this, but excluding the matched patterns. I could grep lines not containing them, but a one command solution would be more appropriate. – Quora Feans Apr 06 '18 at 17:56