Reputation: 1702
How to match the first three lines in the file that contain the word racoon ( from the top to down )
For example
127.0.0.1 localhost
18.2.3.122 racoon133
192.9.200.10 exemachine2
18.2.3.123 Aracoon101
10.10.10.10 jank_machine
18.2.3.124 racoon102
18.2.3.125 start10
18.2.3.125 frt100
18.2.3.128 racoon103
The expected results should be
18.2.3.122 racoon133
18.2.3.123 Aracoon101
18.2.3.124 racoon102
Upvotes: 0
Views: 42
Reputation:
another awk way
awk 'x<x+=/racoon/;x==3{exit}' file
or
awk '/racoon/&&++x;x==3{exit}' file
Upvotes: 2
Reputation: 44063
With awk:
awk '/racoon/ { print; if(++ctr == 3) exit }' filename
Alternatively with sed:
sed -n '/racoon/ { x; /.../ q; s/$/./; x; p; }' filename
...but perhaps most sanely with grep:
grep -m 3 racoon filename
That last one might be a GNU extension; I'm not entirely certain Solaris's grep will accept -m 3
. Of course, there's always
grep racoon filename | head -n 3
although that doesn't short-circuit (could be a performance problem with long files).
Upvotes: 2