只显示的grep的第n匹配 [英] Display only the n'th match of grep

查看:221
本文介绍了只显示的grep的第n匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onefish
onechicken
twofish
twochicken
twocows
threechicken

如果我想对grep含二行,但我只想在第二节比赛。所以,我要的结果twochicken。

What if I want to grep for lines containing "two", but I only want the 2nd match. So I want the result "twochicken".

推荐答案

试试这个:

awk '/two/{i++}i==2' file

与您的数据:

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

这篇关于只显示的grep的第n匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆