【问题标题】:How can I format my grep output to show line numbers at the end of the line, and also the hit count?如何格式化我的 grep 输出以在行尾显示行号以及命中数?
【发布时间】:2011-04-27 10:55:33
【问题描述】:

我正在使用 grep 来匹配文件中的字符串。这是一个示例文件:

example one,
example two null,
example three,
example four null,

grep -i null myfile.txt 返回

example two null,
example four null,

我怎样才能像这样返回匹配的行及其行号:

  example two null, - Line number : 2
  example four null, - Line number : 4
  Total null count : 2

我知道 -c 返回匹配的总行数,但我不知道如何正确格式化它以在前面添加total null count,也不知道如何添加行号。

我能做什么?

【问题讨论】:

    标签: linux bash unix grep


    【解决方案1】:

    -n 返回行号。

    -i 用于忽略大小写。仅在不需要大小写匹配时使用

    $ grep -in null myfile.txt
    
    2:example two null,
    4:example four null,
    

    结合awk打印匹配后的行号:

    $ grep -in null myfile.txt | awk -F: '{print $2" - Line number : "$1}'
    
    example two null, - Line number : 2
    example four null, - Line number : 4
    

    使用命令替换打印出总空计数:

    $ echo "Total null count :" $(grep -ic null myfile.txt)
    
    Total null count : 2
    

    【讨论】:

    • 我可以通过在 : 之后而不是之前添加行号来格式化吗?
    • 您的解决方案看起来不错,但收到错误awk95: syntax error at source line context is >>> ' <<< missing } awk95: bailing out at source line 1
    • 抱歉切换到 linux 现在它的工作:) 它是 windows 版本不太好
    • ...那些说-ni这就是你记住这个技巧的骑士
    【解决方案2】:

    使用-n--line-number

    查看man grep 了解更多选项。

    【讨论】:

    • 新的 linux 用户懒得阅读手册页。但是如果他们使用 linux 足够多,他们就会习惯了 :) 超级好用 :)
    • 并不总是懒惰,(但有时),通常是新 Linux 用户无法理解手册页。 (他们可能看起来神秘)
    • 有时手册页可能需要很多页。这很难全部读完
    【解决方案3】:

    使用grep -n -i null myfile.txt输出每条匹配前的行号。

    我认为 grep 没有打印匹配总行数的开关,但您可以将 grep 的输出通过管道传输到 wc 来完成:

    grep -n -i null myfile.txt | wc -l
    

    【讨论】:

    • -c 将打印匹配的总行数
    • 你是对的。不幸的是,它也会抑制正常输出。
    【解决方案4】:

    或者改用awk

    awk '/null/ { counter++; printf("%s%s%i\n",$0, " - Line number: ", NR)} END {print "Total null count: " counter}' file
    

    【讨论】:

      【解决方案5】:

      grep 查找行并输出行号,但不允许您“编程”其他东西。如果要包含任意文本并进行其他“编程”,可以使用 awk,

      $ awk '/null/{c++;print $0," - Line number: "NR}END{print "Total null count: "c}' file
      example two null,  - Line number: 2
      example four null,  - Line number: 4
      Total null count: 2
      

      或者只使用shell(bash/ksh)

      c=0
      while read -r line
      do
        case "$line" in
         *null* )  (
          ((c++))
          echo "$line - Line number $c"
          ;;
        esac
      done < "file"
      echo "total count: $c"
      

      【讨论】:

        【解决方案6】:

        或在 perl 中(为了完整性...):

        perl -npe 'chomp; /null/ and print "$_ - Line number : $.\n" and $i++;$_="";END{print "Total null count : $i\n"}'
        

        【讨论】:

          【解决方案7】:

          请参考此链接以获取 linux 命令 linux http://linuxcommand.org/man_pages/grep1.html

          要显示行号、代码行和文件,请在终端或 cmd 中使用此命令,GitBash(由终端提供支持)

          grep -irn "YourStringToBeSearch"
          

          【讨论】:

            【解决方案8】:

            只是想我将来可能会对您有所帮助。要搜索多个字符串和输出行号并浏览输出,请键入:

            egrep -ne 'null|three'

            将显示:

            2:example two null,  
            3:example three,  
            4:example four null,   
            

            egrep -ne 'null|three' | less

            将在较少的会话中显示输出

            HTH 六月

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-01-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多