bash - 我可以 grep 获取 grep 结果的相同结果数吗?

标签 bash unix grep

这是我的 grep 命令:

grep 'Query failed' file.txt | grep -o '\bT\w*'

搜索字符串 'Query failed' 然后它给了我以 T 开头的单词。

结果例如:

Test1
Test1
Test2
Test2
Test2
Test3
Test4
Test4

是否有可能得到这样的结果:

Test1 = 2
Test2 = 3
Test3 = 1
Test4 = 2

提前致谢

最佳答案

将其通过管道传输到 sortuniq -c 以计算所有重复。

grep 'Query failed' file.txt | grep -o '\bT\w*' | sort | uniq -c

输出将是:

   2 Test1
   3 Test2
   1 Test3
   2 Test4

从您的示例来看,原始文件似乎已经排序。如果是这样,您可以省略 sort 步骤。

关于bash - 我可以 grep 获取 grep 结果的相同结果数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39109655/

相关文章:

bash - 我可以在不使用 cat 的情况下有条件地将 shell 函数作为管道 "disappear"吗?

报警时多次调用无限循环代码

bash - 将搜索字符串作为 shell 变量传递给 grep

java - GREP 如何只检索包含指定字母的单词?

python - EC2 SSH 连接到其他 SSH 并运行 bash 脚本

Bash 函数检查给定变量是否已设置

bash - 在脚本之间发送信号(bash)

shell - 查找包含字符串的行并使用 shell 脚本将值回显到新行

awk - 如何删除文件夹中多个文件的 "0"行?

javascript - 将所有 TypeScript 文件转译到相对相邻的目录