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 n
th and m
the 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:
- When you see the first
/trigger/
, start a block of commands p
-- print the line:loop
-- set a label namedloop
n
-- get the next linep
-- print the line/trigger/q
-- if the line matches/trigger/
then exitsed
b
-- jump toloop