【问题标题】:linux command grep -is "abc" filename|wc -llinux命令 grep -is "abc" 文件名|wc -l
【发布时间】:2011-02-03 17:02:13
【问题描述】:

s 在那里是什么意思,当管道进入 wc 是什么意思?我知道它最终会计算文件文件名中出现的 abc 的数量,但不确定选项 s 和管道到 wc 的意思

linux command grep -is "abc" filename|wc -l

输出

    47

【问题讨论】:

  • 人会在一分钟内把你送到那里。

标签: linux command


【解决方案1】:

-s 表示“抑制有关不可读文件的错误消息”,而到 wc 的管道表示“获取输出并将其发送到 wc -l 命令”,它有效地计算匹配的行数。您可以使用 grep 的 -c 选项来完成相同的操作: grep -isc "abc" filename

【讨论】:

    【解决方案2】:

    考虑一下,

    command_1 | command_2

    管道的作用是——它获取在它之前写的命令的输出(这里是command_1)并将该输出提供给它之后写的命令(这里是command_2)。

    【讨论】:

      【解决方案3】:

      man 页面包含您想了解的有关 grep 选项的所有信息:

         -s, --no-messages
                Suppress  error  messages about nonexistent or unreadable files.
                Portability note: unlike GNU grep, traditional grep did not con-
                form to POSIX.2, because traditional grep lacked a -q option and
                its -s option behaved like GNU grep's -q option.  Shell  scripts
                intended to be portable to traditional grep should avoid both -q
                and -s and should redirect output to /dev/null instead.
      

      wc -l 的管道可以计算字符串“abc”出现在多少行上。它不一定是字符串在文件中出现的次数,因为一行多次出现只会被计为 1。

      【讨论】:

      • 这真的很清楚并且有帮助,(我在发布问题之前检查了手册页,阅读手册页后仍然感到困惑),谢谢马克!
      • 如果我把-l去掉了,就像grep -is "abc" filename|wc,那么它返回3个数字,比如47 167 285,那么167和285是什么意思呢? (我知道 47 种意思)
      • @maggieqq:行数、字数、字符数。
      • 我知道 167 和 285 是什么意思,但对手册页说 47 是换行符,167 是单词,287 表示字节还是有点困惑,所以 47 行中有 abc 吗?而 167 是 abc 在文件名中出现的次数,准确吗?
      • @maggieqq:没有。在带有 abc 的行上总共有 167 个单词。把管道移到wc,也许会更清楚一点。
      【解决方案4】:

      grep 手册页说:

      -s, --no-messages         suppress error messages
      

      grep 返回包含abc(不区分大小写)的行。您将它们传送到wc 以计算行数。

      【讨论】:

        【解决方案5】:

        来自man grep

           -s, --no-messages
                  Suppress  error  messages about nonexistent or unreadable files.
        

        wc 命令计算行数、单词数和字符数。使用 -l 它返回行数。

        【讨论】:

          猜你喜欢
          • 2015-03-30
          • 1970-01-01
          • 2018-12-01
          • 1970-01-01
          • 2020-05-10
          • 2017-01-12
          • 1970-01-01
          • 1970-01-01
          • 2013-11-21
          相关资源
          最近更新 更多