onefish
onechicken
twofish
twochicken
twocows
threechicken
What if I want to grep for lines containing "two", but I only want the 2nd match. So I want the result "twochicken".
try this:
awk '/two/{i++}i==2' file
with your data:
kent$ echo "onefish
onechicken
twofish
twochicken
twocows
threechicken"|awk '/two/{i++}i==2'
twochicken
note: if your file is huge, do this:
awk '/two/{i++}i==2{print; exit}' file