gpt4 book ai didi

bash - 如何汇总字符串中的数字并生成单个汇总字符串

转载 作者:行者123 更新时间:2023-11-29 09:12:50 27 4
gpt4 key购买 nike

我有一个文件,它是运行测试的结果。在文件中有很多行包含每个测试的结果:

$ grep "tests," results/RunAll_unit_staging-dc1.txt 
1 tests, 2 assertions, 0 failures, 0 errors, 0 skips
1 tests, 7 assertions, 0 failures, 0 errors, 0 skips
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
1 tests, 4 assertions, 0 failures, 0 errors, 0 skips
1 tests, 6 assertions, 0 failures, 0 errors, 0 skips
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
1 tests, 16 assertions, 0 failures, 0 errors, 0 skips
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
8 tests, 152 assertions, 0 failures, 0 errors, 0 skips
1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
$

我想知道是否有一种方法可以将这些行组合成一个汇总行,并汇总总计。在上面的例子中,它看起来像:

22 tests, 213 assertions, 1 failures, 0 errors, 0 skips

我可以像这样 awk 他们生成列中的数字:

$ grep "tests," results/RunAll_unit_staging-dc1.txt | awk '{print $1, $3, $5, $7, $9}'
1 2 0 0 0
1 7 0 0 0
1 2 1 0 0
1 3 0 0 0
1 3 0 0 0
1 3 0 0 0
1 4 0 0 0
1 6 0 0 0
1 3 0 0 0
1 16 0 0 0
1 3 0 0 0
1 3 0 0 0
1 3 0 0 0
8 152 0 0 0
1 3 0 0 0
$

但我不确定从这里到哪里去总结列并将它们放回字符串中。也许我看错了方向。任何帮助表示赞赏

谢谢

一个

最佳答案

你可以试试这个;

awk '/tests/ {sum1+=$1; sum2+=$3;sum3+=$5; sum4+=$7;sum5+=$9;} END {printf "%d tests, %d assertions, %d failures, %d errors,%d skips\n" , sum1 ,sum2,sum3,sum4,sum5}' results/RunAll_unit_staging-dc1.txt

例如;

$ awk '/tests,/ {sum1+=$1; sum2+=$3;sum3+=$5; sum4+=$7;sum5+=$9;} END {printf "%d tests, %d assertions, %d failures, %d errors, %d skips\n" , sum1 ,sum2,sum3,sum4,sum5}' testFile
22 tests, 213 assertions, 1 failures, 0 errors, 0 skips

关于bash - 如何汇总字符串中的数字并生成单个汇总字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40502275/

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