【问题标题】:Displaying the result of two grep commands in bash在 bash 中显示两个 grep 命令的结果
【发布时间】:2020-07-19 04:31:37
【问题描述】:

我正在尝试查找目录中文件名中具有两种不同模式的文件数。我不想要组合计数,而是显示组合结果。

命令 1:find | grep ".coded" | wc -l |输出:4533

命令 2:find | grep ".read" | wc -l |输出:654

寻找的输出:4533 | 654 在一行中

有什么建议吗?谢谢!

【问题讨论】:

  • 快速但脆弱的破解:printf "%s | %s\n" "$(find | grep ".coded" | wc -l)" "$(find | grep ".read" | wc -l)"

标签: bash grep find output


【解决方案1】:

在 bash shell 中使用 process substitutionpr

pr -mts' | ' <(find | grep "\.coded" | wc -l) <(find | grep "\.read" | wc -l)

【讨论】:

    【解决方案2】:

    使用 GNU find,您可以使用 -printf 打印您想要的任何内容,例如,c 用于匹配 .coded 的每个文件,而“r”用于匹配 .read 的每个文件,然后使用awk 来计算你有多少个:

    find -type f \
        \( -name '*.coded*' -printf 'c\n' \) \
        -o \
        \( -name '*.read*' -printf 'r\n' \) \
        | awk '{ ++a[$0] } END{ printf "%d | %d\n", a["c"], a["r"] }'
    

    顺便说一句,你的 grep 模式匹配 XcodedYread,或者你的时期的任何东西;如果是文字句点,则必须对其进行转义,如'\.coded''\.read'。此外,如果您的文件名包含换行符,则您的计数将被取消。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2018-08-17
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多