【发布时间】:2020-07-07 08:54:13
【问题描述】:
我有几个行大小不同的文件,但每个文件中的列数是相同的。例如
ifile1.txt
1 1001 ? ?
2 1002 ? ?
3 1003 ? ?
4 1004 ? ?
5 1005 ? 0
6 1006 ? 1
7 1007 ? 3
8 1008 5 4
9 1009 3 11
10 1010 2 9
ifile2.txt
1 2001 ? ?
2 2002 ? ?
3 2003 ? ?
4 2004 ? ?
5 2005 ? 0
6 2006 6 12
7 2007 6 5
8 2008 9 10
9 2009 3 12
10 2010 5 7
11 2011 2 ?
12 2012 9 ?
ifile3.txt
1 3001 ? ?
2 3002 ? 6
3 3003 ? ?
4 3004 ? ?
5 3005 ? 0
6 3006 1 25
7 3007 2 3
8 3008 ? ?
在每个文件中,第一列表示索引号,第二列表示 ID。 我想从第 3 列开始计算每个索引号的平均值。
想要的输出:
1 ? ? ---- [Here ? is computed from ?, ?, ?] So answer is ?
2 ? 6.0 ---- [Here 6 is computed from ?, ?, 6] So answer is 6/1=6.0
3 ? ?
4 ? ?
5 ? 0.0
6 3.5 12.7
7 4.0 3.7
8 7.0 7.0 ----- [Here 7 is computed from 5, 9, ? ] So answer is 14/2=7.0
9 3.0 11.5
10 3.5 8.0
11 2.0 ?
12 9.0 ?
【问题讨论】:
-
but it is taking all "?" as 0.所以检查一下。如果s[i]中的所有元素都等于?,则输出?。 IE。all_questions=1; for (i in s) if (s[i] != "?") all_questions=0然后是if (all_questions) print "?" else print <the avarage> -
某行不是“?”如果 c[i]>0。
awk '{n[$1]++}$3!="?"{s[$1]+=$3;c[$1]++;} END{for(i in n)print i,(c[i]?sprintf("%.1f",s[i]/c[i]):"?");}' ifile*