2014-10-20 173 views
1

我想弄清楚什麼grep語法將得到一個\獨特的搜索結果。Grep正則表達式第一次匹配後停止

例如:

的grep 「^嚴厲」 server.out

重度:無法連接庫,呼叫服務PING_SERVER時出錯。 SEVERE:無法連接資源庫,調用服務PING_SERVER時發生錯誤。 SEVERE:無法連接資源庫,調用服務PING_SERVER時發生錯誤。

我想輸出只顯示該事件的第一個查找。

任何幫助將是偉大的!

  • tcwbot

回答

2

GNU的grep 2.16附帶的cygwin有這個選項(從 '人的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. When grep stops after NUM matching 
      lines, it outputs any trailing context lines. When the -c or 
      --count option is also used, grep does not output a count 
      greater than NUM. When the -v or --invert-match option is also 
      used, grep stops after outputting NUM non-matching lines. 

所以嘗試:

$ grep -m 1 "^SEVERE" server.out 
相關問題