我有一个运行测试结果的文件。在文件中包含许多行以及每个测试的结果:
$ 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
我可以让他们在列中生成数字,如下所示:
$ 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
$
但我不知道从这里去哪里总结列并将它们放回字符串中。也许我正朝错误的方向看。任何帮助表示赞赏
THX
A
答案 0 :(得分:3)
你可以试试这个;
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
EG;
$ 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
答案 1 :(得分:1)
可以使用perl
perl -lne 'while(/(\d+) (\w+)/g){$x{$2}+=$1}}{print join(", ",map{"$_ $x{$_}"}keys %x)' f
failures 1, skips 0, errors 0, tests 22, assertions 213