gpt4 book ai didi

bash - 如何在 bash 脚本中使用 uniq -cd 并只提取计数而不提取行?

转载 作者:行者123 更新时间:2023-11-29 09:22:07 25 4
gpt4 key购买 nike

我有一个 .sh 文件,它获取日志文件并提取数据并生成报告。我想计算错误弹出窗口占总行数的百分比(最多的人)。

到目前为止我有这个:

awk '// {print $4, substr($0, index($0,$9))}' | sort \
| uniq -cd | sort -nr | head -n20 > $filename-sr1.tmp

这会输出两列,计数后跟行。

我怎样才能只用计数来进行计算。例如。 计数/total_lines = 0.000000...

最佳答案

这里仅使用 awk,尽管输出顺序是任意的,因此您可能希望将其通过管道传递给 sort -n

$ cat file
foo
foo
bar
foo
quux
quux
$ awk '{a[$0]++} END{for (i in a) if (a[i]>1) printf "%5.2f%%\t%s\n", 100*a[i]/NR, i}' file
33.3% quux
50.0% foo

并调整您当前的 awk:

awk '{a[$4" "substr($0, index($0,$9))]++} END{for (i in a) if (a[i]>1) printf "%5.2f%%\t%s\n", 100*a[i]/NR, i}'
# or possibly
awk '{s=$4; for(i=9;i<=NF;++i) s=s" "$i; a[s]++} END{for (i in a) if (a[i]>1) printf "%5.2f%%\t%s\n", 100*a[i]/NR, i}'

关于bash - 如何在 bash 脚本中使用 uniq -cd 并只提取计数而不提取行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11959867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com