1

This question already has an answer here:

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.

marked as duplicate by don_crissti, αғsнιη, Kusalananda, Timothy Martin, G-Man Apr 6 '18 at 20:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @don_crissti: almost, I want the same, but excluding the two patterns. – Quora Feans Apr 6 '18 at 17:55
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
  • 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 6 '18 at 17:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.