Reputation: 1007
Let's say I have the following file:
* cat
* dog
* cat
* fish
* fish
* cat
* turtle
Let's say I want to find the line number of the second match for cat, how can I do that?
Upvotes: 2
Views: 1214
Reputation: 165
The awk solutions are more I/O efficient but I wanted to point out there is a pure shell solution also:
grep -n cat | head -2 | tail -n 1 | cut -d: -f1
Feel free to substitute any other regular expression for cat.
Upvotes: 1
Reputation: 67567
$ awk '/cat/{c++} c==2{print NR;exit}' file
3
count the cats, print the line number and exit after the required match value.
Upvotes: 6