Grep只在第一场比赛就停止了

本文翻译自:Grep only the first match and stop

I'm searching a directory recursively using grep with the following arguments hoping to only return the first match. 我正在使用grep以下列参数递归搜索目录,希望仅返回第一个匹配项。 Unfortunately, it returns more than one -- in-fact two the last time I looked. 不幸的是,它返回的不止一个-实际上是我上次查看时的两个。 It seems like I have too many arguments, especially without getting the desired outcome. 似乎我有太多争论,尤其是没有得到理想的结果。 :-/ :-/

# grep -o -a -m 1 -h -r "Pulsanti Operietur" /path/to/directory

returns: 返回:

Pulsanti Operietur
Pulsanti Operietur

Maybe grep isn't the best way to do this? 也许grep不是执行此操作的最佳方法? You tell me, thanks very much. 你告诉我,非常感谢。


#1楼

参考:https://stackoom.com/question/X8lK/Grep只在第一场比赛就停止了


#2楼

-m 1 means return the first match in any given file. -m 1表示返回任何给定文件中的第一个匹配项。 But it will still continue to search in other files. 但是它仍然会继续在其他文件中搜索。 Also, if there are two or more matched in the same line, all of them will be displayed. 另外,如果同一行中有两个或多个匹配项,则将全部显示。

You can use head -1 to solve this problem: 您可以使用head -1解决此问题:

grep -o -a -m 1 -h -r "Pulsanti Operietur" /path/to/dir | head -1

explanation of each grep option: 每个grep选项的说明:

-o, --only-matching, print only the matched part of the line (instead of the entire line)
-a, --text, process a binary file as if it were text
-m 1, --max-count, stop reading a file after 1 matching line
-h, --no-filename, suppress the prefixing of file names on output
-r, --recursive, read all files under a directory recursively

#3楼

My grep-a-like program ack has a -1 option that stops at the first match found anywhere. 我的类似grep-a的程序ack具有-1选项,该选项在任何地方的第一个匹配项处停止。 It supports the -m 1 that @mvp refers to as well. 它也支持@mvp引用的-m 1 I put it in there because if I'm searching a big tree of source code to find something that I know exists in only one file, it's unnecessary to find it and have to hit Ctrl-C. 我把它放在那里是因为如果我正在搜索一棵大的源代码树以找到仅存在于一个文件中的已知内容,则无需找到它并且必须按Ctrl-C。


#4楼

单个衬里,使用find

find -type f -exec grep -lm1 "PATTERN" {} \; -a -quit

#5楼

You can pipe grep result to head in conjunction with stdbuf . 您可以将grep结果与stdbuf一起传递head

Note, that in order to ensure stopping after Nth match, you need to using stdbuf to make sure grep don't buffer its output: 请注意,为了确保在第N个匹配后停止,您需要使用stdbuf来确保grep不缓冲其输出:

stdbuf -oL grep -rl 'pattern' * | head -n1
stdbuf -oL grep -o -a -m 1 -h -r "Pulsanti Operietur" /path/to/dir | head -n1
stdbuf -oL grep -nH -m 1 -R "django.conf.urls.defaults" * | head -n1

As soon as head consumes 1 line, it terminated and grep will receive SIGPIPE because it still output something to pipe while head was gone. 一旦head消耗了1行,它就会终止,并且grep将收到SIGPIPE因为它在head走后仍然输出一些内容给管道。

This assumed that no file names contain newline. 假定没有文件名包含换行符。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值