Bash 计算shell脚本中每行的平均和
我不熟悉shell脚本,需要一些帮助: 我有以下输入格式:Bash 计算shell脚本中每行的平均和,bash,shell,awk,Bash,Shell,Awk,我不熟悉shell脚本,需要一些帮助: 我有以下输入格式: x,x,12,4,61,1,9 y,y,13,4,62,2,9 z,z,11,4,61,1,9 c,c,11,4,60,3,9 我想显示每行的平均和和和第一个字,例如: x x : 17.4 y y: 18 ... 到目前为止,我的代码如下所示: #!/bin/sh while IFS= read -r line; do echo "$line" done < "input.txt" #/垃圾箱/垃圾箱 而IFS=读取-r
x,x,12,4,61,1,9
y,y,13,4,62,2,9
z,z,11,4,61,1,9
c,c,11,4,60,3,9
我想显示每行的平均和和和第一个字,例如:
x x : 17.4
y y: 18
...
到目前为止,我的代码如下所示:
#!/bin/sh
while IFS= read -r line; do
echo "$line"
done < "input.txt"
#/垃圾箱/垃圾箱
而IFS=读取-r行;做
回音“$line”
完成<“input.txt”
但是我不知道如何处理每一行如果您使用awk,它将为您循环记录:
$ awk -F, '{ # set input field delimiter
for(i=3;i<=NF;i++) # iterate values starting from 3rd field
sum+=$i # sum up the values
print $1,$2,":",sum/(NF-2) # output
sum=0
}' file
如果您想知道NF
:
NF
The number of fields in the current input record.
NF is set each time a new record is read,
when a new field is created,
or when $0 changes
请告诉我们每行输入的字段数是否固定。对不起,这不是StackOverflow的工作方式。形式为“我想做X,请给我提示和/或示例代码”的问题被认为是离题的。请访问并阅读,尤其是阅读woooh。。。工作得很有魅力!还有一个方面,如果我想按字母顺序排序。。。我应该在那之后用管道分拣nk2吗?多亏了一点,将输出传送到
sort
才是最简单的方法。
NF
The number of fields in the current input record.
NF is set each time a new record is read,
when a new field is created,
or when $0 changes