117
A
回答
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$
相關問題
- 1. 返回結果數量限制
- 2. 如何查詢而不限制返回結果的數量?
- 3. 如何獲取MySql無限制返回的結果數量?
- 4. 限制返回結果
- 5. 如何限制從algolia返回的結果屬性?
- 6. 限制在CloudKit中返回的結果數量
- 7. 限制Solr中過濾器查詢返回的結果數量
- 8. 限制彈性搜索返回結果的數量
- 9. 限制從Linq列表中返回的結果的數目
- 10. 如何限制本C#程序中返回的Google搜索結果的數量?
- 11. jQuery的限制結果/選擇返回
- 12. Skydrive API - 限制返回的結果?
- 13. GroupBy返回超出限制的結果
- 14. MYSQL限制返回意外的結果
- 15. 對返回的文本的grep限制
- 16. 從數據庫返回的限制結果
- 17. imap_sort限制結果數量?
- 18. 返回結果的一部分(GREP)
- 19. BSD grep返回不正確的結果
- 20. SQL LIMIT返回任何結果,其中沒有限制返回結果
- 21. 限制結果serachableService.moreLikeThis返回在Grails中?
- 22. SQL限制返回所有結果
- 23. 爲什麼$ cursor返回45限制時,我將返回結果的數量限制爲40
- 24. 如何從VBA函數返回結果
- 25. 如何從函數返回json結果?
- 26. 找到與grep不返回結果
- 27. 使用grep結果 - 返回值
- 28. 從grep的結果
- 29. 從函數返回結果
- 30. 從ViewResult返回的限制結果爲10
你的情況,你不希望計算機上下功夫。但如果只是一個人的可讀性問題,你可以使用`少`通過管道。這將填充屏幕,你可以按ENTER鍵看到更多的行和`q`退出:`grep「SomeText」somefile.csv | less` – SilentSteel 2014-12-22 17:49:43
您可能想要將接受的答案更改爲http://stackoverflow.com/a/5013198/2707864。您接受的人可能無法工作,和/或變得非常緩慢(請參閱相應的評論)。 – 2015-11-04 08:28:24