how can I get sed to quit after the first matching address range?

While jason's ans is what you're looking for, I would have preferred using awk for this task

awk '/trigger/{p++} p==2{print; exit} p>=1' file

Output:

trigger
text1
text2
trigger

This would provide more flexibility to chose lines between nth and mthe occurrence of trigger.

E.g.

$ awk -v n=2 -v m=3 '/trigger/{p++} p==m{print; exit} p>=n' file
trigger
text5
trigger

You can do this with a loop in GNU sed:

sed -n '/trigger/{p; :loop n; p; /trigger/q; b loop}'

Explanation:

  1. When you see the first /trigger/, start a block of commands
  2. p -- print the line
  3. :loop -- set a label named loop
  4. n -- get the next line
  5. p -- print the line
  6. /trigger/q -- if the line matches /trigger/ then exit sed
  7. b -- jump to loop

Tags:

Regex

Sed