[英]Awk - using an array with multiple files
I'm new to shell scripting, and I need to compute an average file1, and then the average of the result and a number in file2, so far i came up with this, but it doesn't print anything.我是 shell 脚本的新手,我需要计算平均 file1,然后计算结果的平均值和 file2 中的数字,到目前为止我想出了这个,但它没有打印任何东西。
awk '{if ($FILENAME == "spring") array[$1]=($2+$3+$4+$5+$6+$7+$8)/7; if($FILENAME == "fall") array[$1]=(array[$1]+$2)/2 } END { for (var in array) print var,array[var]}' ./spring ./fall
Any way to solve this problem?有什么办法可以解决这个问题?
There are no sigils in awk. awk 中没有符号。 Try dropping the
$
:尝试删除
$
:
awk 'FILENAME ~ /spring/ { array[$1]=($2+$3+$4+$5+$6+$7+$8)/7 }
FILENAME ~ /fall/ { array[$1]=(array[$1]+$2)/2 }
END { for (var in array) print var,array[var]}' ./spring ./fall
In short, FILENAME
is the name of the file currently being processed, but $FILENAME
is equivalent to $0
when FILENAME starts with a letter.简而言之,
FILENAME
是当前正在处理的文件的名称,但是当 FILENAME 以字母开头时, $FILENAME
相当于$0
。
How about awk '{s+=$1}ENDFILE{print FILENAME,s/FNR;s=0}' RS=" " file1 file2
: awk '{s+=$1}ENDFILE{print FILENAME,s/FNR;s=0}' RS=" " file1 file2
怎么样:
$ cat file1
1 2 3 4 5 6 7 8
$ cat file2
1 2
$ awk '{s+=$1}ENDFILE{print FILENAME,s/FNR;s=0}' RS=" " file1 file2
file1 4.5
file2 1.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.