如何将所有其他列加在一起

How to add every other columns together

我有一个文件(总共 72 列),我想添加从第 4 列开始的所有其他列,

infile

20170101 1 1 1.5 2 2 3 3  
20170101 2 1 2 2 4 3 4
20170101 3 1 5 2 3 3 6

输出应该是

20170101 1  6.5
20170101 2 10
20170101 3 14

这是我有的,但是行不通。

 awk '{for(i=4;i<=NF;i+=2) sum[i]+=$i; print}' infile

感谢您的帮助。

以下简单的 awk 可以帮助您解决这个问题。

awk '{for(i=4;i<=NF;i+=2){sum+=$i};print ,,sum;sum=0}'   Input_file

现在也添加 non-one 线性形式的解决方案。

awk '
{
  for(i=4;i<=NF;i+=2){  sum+=$i  };
  print ,,sum;
  sum=0
}
'   Input_file