2011-02-16 35 views
117

我想說grep最多10行。如何限制從grep返回的結果數量?

我不想讓自己的電腦努力工作。我希望它在grep發現10個結果後停止。可能嗎?

+0

你的情況,你不希望計算機上下功夫。但如果只是一個人的可讀性問題,你可以使用`少`通過管道。這將填充屏幕,你可以按ENTER鍵看到更多的行和`q`退出:`grep「SomeText」somefile.csv | less` – SilentSteel 2014-12-22 17:49:43

+2

您可能想要將接受的答案更改爲http://stackoverflow.com/a/5013198/2707864。您接受的人可能無法工作,和/或變得非常緩慢(請參閱相應的評論)。 – 2015-11-04 08:28:24

回答

168

-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的停止讀取文件一次指定比賽數量已經找到了!

61

另一種選擇是隻用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.

+3

請注意,您不能使用`|當`grep`與`-A`或`-B`(因此不僅搜索結果(`-o`),而且也用於上下文)時使用頭部管道。在這種情況下,你只剩下`-m`來告訴grep返回結果的行數。 – 2011-08-30 15:20:06

+16

使用頭並不實際阻止grep在整個文件中運行。在grep中使用-m選項。 – LopSae 2012-04-09 06:29:36

6

awk中的方法:

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

使用尾巴:

#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) 
[email protected]:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | head 2 
head: cannot open ‘2’ for reading: No such file or directory 
[email protected]:~/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) 
[email protected]:~/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) 
[email protected]:~/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) 
[email protected]:~/bugs/navencrypt/dev-tools$