如何限制从grep返回结果的数量?

我想说从grep最多10行。

我不希望我的电脑努力工作。 我希望它在grepfind10个结果后停止。 可能吗?

-m选项可能是你要找的东西:

 grep -m 10 PATTERN [FILE] 

另一个select是使用头 :

 grep ...parameters... yourfile | head 

这将不需要search整个文件 – 当find前十个匹配行时,它将停止。 这种方法的另一个优点是,即使使用grep和-o选项,也不会超过10行。

例如,如果该文件包含以下行:

 112233 223344 123123 

那么这是输出的差异:

 $ grep -o'1.'  yourfile | 头-n2
 11
 12

 $ grep -m2 -o'1.'
 11
 12
 12

使用head只会返回2个结果,而-m2返回3。

Awk方法:

 awk '/pattern/{print; count++; if (count==10) exit}' file 

使用尾巴:

 #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$