在大型文件中遇到第一个模式匹配后,如何使grep停止? [英] How can I make grep stop after hitting a first pattern match in a large file?

查看:292
本文介绍了在大型文件中遇到第一个模式匹配后,如何使grep停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多这样的行:

a|b|c
e|f|2
h|i|j

我想在第三列中找到仅包含数字的第一行(在示例中为第二列).我该如何在不转储所有数据并将其通过管道传递到head的情况下进行复制?

I want to find the FIRST row with numbers ONLY in the 3rd column (that would be the 2nd one in the example). How can I grep that without dumping all data and pipe it into head?

推荐答案

您可以在grep中使用-m1选项:

You can use -m1 option in grep:

grep -m1 "[0-9]$" file

根据man grep:

 -m num, --max-count=num
         Stop reading the file after num matches.

或更准确地说,是使用awk做到这一点:

Or more accurately do it using awk:

awk -F'|' '$3 ~ /^[0-9]+$/{print; exit}' file
e|f|2

这篇关于在大型文件中遇到第一个模式匹配后,如何使grep停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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