在awk输出中添加行



我正在使用awk编写一个bash脚本,该脚本输出出现在名为output.txt的文本文件中的400 - 500范围内的错误代码计数

awk '($9 >= 400)' output.txt | awk '{print $9}' | sort | uniq -c

上述输出为:

12 400
11 401
55 403
91 404
41 500

如何使用bash将第一列添加在一起,以便在上面的示例中获得210而不是上面的输出。。。(12+11+55+91+41=210(

如果我想通过命令行参数而不是output.txt在文件中输入,我应该如何编辑脚本?我知道您使用"$1"one_answers"$2"来访问命令行参数,但在这种情况下,考虑到我已经在awk中使用$9,它将如何工作

要立即解决您的问题,因为您已经使用了1个命令,可以尝试如下(尽管我很确定,如果您可以分享您的输入样本,我们可能也可以在一个命令中完成(:

your_command | awk '{sum+=$1} END{print sum}'

同样在OP的命令部分,awk '($9 >= 400)' output.txt | awk '{print $9}'可以缩短为awk '($9 >= 400){print $9}' output.txt

假设每个错误代码在$9中,要获得the count of error codes ranging from 400 - 500 that appear in a text file called output.txt,请执行以下操作:

awk '($9 >= 400) && ($9 <= 500) && !seen[$9]++{cnt++} END{print cnt+0}' output.txt

若要获得the count of **lines containing** error codes ranging from 400 - 500 that appear in a text file called output.txt,假设每个错误代码都在$9中,则为:

awk '($9 >= 400) && ($9 <= 500){cnt++} END{print cnt+0}' output.txt

最新更新