bash 两个不同文件中的总和数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15986284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum number in two different files
提问by Valerio D. Ciotti
I have two files of same lenght like:
我有两个相同长度的文件,例如:
0.99952 0.01
0.98927 0.02
0.98242 0.03
and
和
-0.758 0.01
-0.745 0.02
-0.742 0.03
I would like to sum the first column numbers and print it in a new file with the same elements alongside in the second column. The output should be:
我想对第一列编号求和并将其打印在一个新文件中,第二列中的元素相同。输出应该是:
0.24152 0.01
0,24427 0.02
0,24042 0.03
I tried with
我试过
paste file1 file2 | awk '{print ( + ), }' > sum
but the output is:
但输出是:
1.00952 0.01
1.00927 0.02
1.01242 0.03
回答by loentar
You taking invalid parameter number in awk ($2 instead of $3)
您在 awk 中使用了无效的参数号($2 而不是 $3)
0.99952 0.01 -0.758 0.01
0.98927 0.02 -0.745 0.02
0.98242 0.03 -0.742 0.03
There is a working example:
有一个工作示例:
paste data1 data2 | awk '{print ( + ), }'
回答by Chris Seymour
The single awkapproach:
单一awk方法:
$ awk 'NR==FNR{a[NR]=;next}{print +a[FNR],}' file1 file2
0.24152 0.01
0.24427 0.02
0.24042 0.03

