除第一列外,所有列每两行的平均值 [英] Average of every two lines for all columns except the first one

查看:82
本文介绍了除第一列外,所有列每两行的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据集:

I have a dataset that looks like this:

item1 20 30 12
item1 10 50 17
item2 -9 112 15
item2 -9 100 10

item1 20 30 12
item1 10 50 17
item2 -9 112 15
item2 -9 100 10

实际数据集有101列.我想在每列中打印连续值的平均值(第一列除外,其中包含名称).

The actual dataset has 101 columns. I want to print the average of consecutive values in each column (except the first column which contains names).

所以预期的输出将是

item1 15 40 14.5
item2 -9 106 12.5

item1 15 40 14.5
item2 -9 106 12.5

我从此链接中找到我可以使用以下代码对单个列执行此操作

I found from this link that I can do this for a single column with the following code

awk '{sum+=$2}(NR%2)==0{print sum/2; sum=0;}'

但是我无法弄清楚其余列如何执行此操作,并不能为平均值行打印唯一的行名(例如item1).我尝试过这样的事情:

But I cannot figure out how to do this for rest of the columns and print the unique row name (e.g: item1) for rows of average values. I tried something like this:

awk '{for(i=2;i<=NF;i++) sum[i]+=$i} NR%2==0 {print sum[i]/2;sum[i]=0}'

但是代码显然是不正确的,如果有人能指出我在做什么错以及如何改进它以获得预期的结果,我将不胜感激.谢谢!

but the code is obviously incorrect and I will appreciate if anyone could point out what am I doing wrong and how to improve it to get expected result.Thanks!

推荐答案

从第2行开始的连续两行行之和为动态字段数的总和:

Sum consecutive pairs of lines starting at line 2 for a dynamic number of fields:

$ awk 'NR>1{for(i=2;i<=NF;i++)a[i]+=$i;if(NR%2){printf "%s ",n$1;n="\n";for(i=2;i<=NF;i++)printf "%s ",a[i]/2;delete a}}' file
item1 15 40 14.5 
item2 -9 106 12.5 

这篇关于除第一列外,所有列每两行的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆