Reputation: 15340
I have the following file:
$ cat file
> First line
> Second line
> Third line
> Fourth and last line
> First line
> Second line
> Third line
> Fourth and last line
I want to print the first 3 lines, easy:
$ sed -n '1,3p' file
> First line
> Second line
> Third line
Now I want to print from the occurrence First
to the occurrence Third
:
$ sed -n '/First/,/Third/p' file
> First line
> Second line
> Third line
> First line
> Second line
> Third line
Ah! Not quite what I wanted, I want only the first occurrence of the matched pattern range. How can I do that when I have regex' as my addresses?
Upvotes: 1
Views: 160
Reputation: 47189
Append the end-pattern as a quit condition:
sed -n '/First/,/Third/p; /Third/q' file
Output:
> First line
> Second line
> Third line
Upvotes: 3
Reputation: 41460
You can do like this with awk
awk '!f; /Third/ {f=1}' file
> First line
> Second line
> Third line
Or even shorter and better, since it stops processing file after found.
awk '1; /Third/ {exit}' file
Or if its needed to take from first
to third
awk '/First/ {f=1} f; /Third/ {exit}' file
Upvotes: 2
Reputation: 2486
I believe awk may help you to do this
awk '/First/{found=1} found{print; if(/Third/) exit}' file
Upvotes: 2
Reputation: 14949
You can try this sed
:
sed -n '/First/{:loop; $!N; /Third/{p;q}; b loop;}' file
Upvotes: 2