我希望从grep命令中最多输出10行结果。
我不想让我的计算机过度运转,我想要它在grep找到10个结果后停止。这种操作是否可行?
-m
选项可能是你要找的:
grep -m 10 PATTERN [FILE]
从 man grep
中获取:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input is
standard input from a regular file, and NUM matching lines are
output, grep ensures that the standard input is positioned to
just after the last matching line before exiting, regardless of
the presence of trailing context lines. This enables a calling
process to resume a search.
注意:grep在找到指定数量的匹配项后就会停止读取文件!
tail
通常是可行的,但如果你使用带上下文的grep
,比如grep -A10 PATTERN
,就会出现问题。使用tail
会截断上下文而不是结果数量。这个答案符合我的需求。 - dimo414-m 10
是一个在搜索多个文件时起作用的选项!如果第一个文件中有太多匹配项,使用管道命令显示前几行将不会显示后续文件中的匹配项。谢谢! - Julien-m
选项与-A/-B/-C
选项冲突,仅输出m
行而不是由-A/-B/-C
定义的内容。新版本没有这个问题(在Arch上测试了3.1版本)。 - Nikolaos Kakouros另一个选择是直接使用head命令:
grep ...parameters... yourfile | head
这不需要搜索整个文件-当找到第十行匹配的行时,它将停止。使用这种方法的另一个优点是,即使您使用了带有-o选项的grep,它也最多返回10行。
例如,如果文件包含以下行:
112233
223344
123123
这是输出结果的不同之处:
$ grep -o '1.' yourfile | head -n2 11 12 $ grep -m2 -o '1.' 11 12 12
使用 head
只返回所需的 2 个结果,而 -m2 返回了 3 个。
grep
与-A
或-B
一起时(因此不仅搜索结果(-o
),而且还要搜索上下文),您不能使用| head
管道。在这种情况下,您只能使用-m
告诉grep返回具有结果的行数。 - Attila O.Awk方法:
awk '/pattern/{print; count++; if (count==10) exit}' file
针对两种情况:
grep -m 2
是每个文件的最大匹配次数。git grep
,它不带有-m
选项。在这些情况下,一个好的替代方案是使用grep | sed 2q
来在所有文件中找到前两个匹配项。Sed文档:https://www.gnu.org/software/sed/manual/sed.html
我经常使用
git grep
,它不需要-m
。
实际上,从Git 2.38(2022年第三季度)开始:
"git grep -m<max-hits>
"(手册)是一种限制每个文件显示的匹配项数量的方法。
这意味着在Git存储库中进行搜索时,可以使用git grep -m
作为grep
的替代方法。
00xc
)。gitster
-- 的 提交 8c4f65e,2022年7月13日)
这个补丁增加了一个命令行选项,类似于GNU grep(1)的
grep
:添加 --max-count 命令行选项签名:Carlos López 00xc@protonmail.com
-m
/ --max-count
,用户可能已经习惯了使用它。-C
(显示代码上下文)或-p
(显示包含函数))的同时限制输出中显示的匹配数量成为可能,这将很难通过shell管道(例如head(1)
)实现。git grep
在其man页面中包括了这个选项。#dmesg
...
...
...
[132059.017752] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
[132116.566238] cfg80211: Calling CRDA to update world regulatory domain
[132116.568939] cfg80211: World regulatory domain updated:
[132116.568942] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
[132116.568944] cfg80211: (2402000 KHz - 2472000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
[132116.568945] cfg80211: (2457000 KHz - 2482000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
[132116.568947] cfg80211: (2474000 KHz - 2494000 KHz @ 20000 KHz), (300 mBi, 2000 mBm)
[132116.568948] cfg80211: (5170000 KHz - 5250000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
[132116.568949] cfg80211: (5735000 KHz - 5835000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
[132120.288218] cfg80211: Calling CRDA for country: GB
[132120.291143] cfg80211: Regulatory domain changed to country: GB
[132120.291146] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
[132120.291148] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291150] cfg80211: (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291152] cfg80211: (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291153] cfg80211: (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
[132120.291155] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | head 2
head: cannot open ‘2’ for reading: No such file or directory
alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -2
[132120.291153] cfg80211: (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
[132120.291155] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -5
[132120.291148] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291150] cfg80211: (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291152] cfg80211: (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291153] cfg80211: (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
[132120.291155] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -6
[132120.291146] cfg80211: (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
[132120.291148] cfg80211: (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291150] cfg80211: (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291152] cfg80211: (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291153] cfg80211: (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
[132120.291155] cfg80211: (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
alex@ubuntu:~/bugs/navencrypt/dev-tools$
less
。这将填充屏幕,您可以按 ENTER 查看更多行,并按q
退出:grep "SomeText" somefile.csv | less
。 - SilentSteel