第一场比赛后Grep正则表达式停止

时间:2014-10-20 21:57:06

标签: grep

我试图弄清楚grep语法是什么来获得一个\唯一的搜索结果。

例如:

  

grep“^ SEVERE”server.out

严重:无法连接存储库,调用服务PING_SERVER时出错。 严重:无法连接存储库,调用服务PING_SERVER时发生错误。 严重:无法连接存储库,调用服务PING_SERVER时发生错误。

我希望输出只显示该事件的第一个发现。

任何帮助都会很棒!

  • tcwbot

1 个答案:

答案 0 :(得分:2)

cygwin附带的GNU grep 2.16有这个选项(来自'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.  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