【问题标题】:Count matching occurrences in a single line solaris在单行 solaris 中计算匹配的出现次数
【发布时间】:2019-02-27 00:42:00
【问题描述】:

当存在单行的长字符串时,任何方便的命令都可用于 grep 匹配字符串并计算字符串出现的次数。

"grep -i something abc.txt | wc -l" 在多行时工作正常,但如果 abc.txt 有单行且多次出现,我们无法获得计数。

PS:我已经阅读了关于这个问题的多个链接,但没有一个对我有用,因为我使用的是 Sun OS。请记住 Sun OS。

谢谢

【问题讨论】:

    标签: shell unix scripting solaris sunos


    【解决方案1】:

    awk 怎么样:

    $ cat file
    something. something else
    not that's something
    $ awk 'i=gsub(/something/,"&"){c+=i}END{print c}' file
    3
    

    如果它是较长字符串 (something.) 的一部分,则会计算匹配项,例如 grep

    【讨论】:

    • @Stunner 因为你不熟悉。
    【解决方案2】:

    您的grep 是否支持选项-o?在这种情况下,您可以使用

    grep -oi something abc.txt | wc -l
    

    当这不起作用时。尝试一种解决方法,例如

    sed 's/something/&\n/g' abc.txt | grep -ci "something"
    

    【讨论】:

      【解决方案3】:

      在 Solaris 11 上,使用 /usr/gnu/bin/grep 使您使用 -o 选项在其他地方找到的答案有效。

      【讨论】:

      • /usr 下没有 gnu 文件夹,下面是 whereis grep grep 的输出:/usr/bin/grep /usr/man/man1/grep.1
      • 您使用的是什么版本的 Solaris?
      • 对不起,迟到的回复.. 我仍然没有找到答案。这是 Sun OS 5.10
      • SunOS 5.10 是 Solaris 10,因此与 Solaris 11 中当前提供的软件相比,您将拥有更旧且更少的软件。
      猜你喜欢
      • 2013-12-25
      • 2012-08-09
      • 1970-01-01
      • 2020-07-05
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      相关资源
      最近更新 更多