awk來保存(考慮到你的INPUT_FILE排序模式下,如果沒有,那麼你可以下面的代碼之前使用sort -t, -k1 | awk ...
太):
awk -F, 'prev && prev != $1{for(i in a){split(i, b," ");val[b[1]]=val[b[1]]?val[b[1]] FS sprintf("%0.2f",a[i]/c[i]):sprintf("%0.2f",a[i]/c[i]);};delete a;print b[1],val[b[1]]}{a[$1,2]+=$2;a[$1,3]+=$3;a[$1,4]+=$4;c[$1,2]++;c[$1,3]++;c[$1,4]++;prev=$1} END{for(i in a){split(i, b," ");val[b[1]]=val[b[1]]?val[b[1]] FS sprintf("%0.2f",a[i]/c[i]):sprintf("%0.2f",a[i]/c[i]);};delete a;print b[1],val[b[1]]}' SUBSEP=" " Input_file
輸出將如下。
0.5 4.18,0.77,0.01
0.6 3.74,0.77,0.01
0.7 3.84,0.78,0.02
現在也增加一種非線性形式的解決方案。
awk -F, '
prev && prev != $1{
for(i in a){
split(i, b," ");
val[b[1]]=val[b[1]]?val[b[1]] FS sprintf("%0.2f",a[i]/c[i]):sprintf("%0.2f",a[i]/c[i]);
};
delete a;
print b[1],val[b[1]]
}
{
a[$1,2]+=$2;
a[$1,3]+=$3;
a[$1,4]+=$4;
c[$1,2]++;
c[$1,3]++;
c[$1,4]++;
prev=$1
}
END{
for(i in a){
split(i, b," ");
val[b[1]]=val[b[1]]?val[b[1]] FS sprintf("%0.2f",a[i]/c[i]):sprintf("%0.2f",a[i]/c[i]);
};
delete a;
print b[1],val[b[1]]
}
' SUBSEP=" " Input_file
編輯:添加的命令解釋過了。
awk -F, '
##making field seprator as comma(,)
prev && prev != $1{
##Checking here if value of prev variable is NOT equal to first column and value of variable prev is NOT NULL.
for(i in a){
##Traversing in array named a now.
split(i, b," ");
##using split utility of awk which will split any variable or line to an array with provided delimiter eg--> split(variable/line, array_name,delimiter), like i(index of array a) is provided here to be splited into array named b with delimiter as a space.
val[b[1]]=val[b[1]]?val[b[1]] FS sprintf("%0.2f",a[i]/c[i]):sprintf("%0.2f",a[i]/c[i]);
##creating an array named val with index of array b value whose value will be the AVG/MEAN of all $1s and its index will be $1. It will concatenate its own value.
};
delete a;
##Deleting array a here.
print b[1],val[b[1]]
##printing array b whose index is 1 and array val whose index is value of b[1] array.
}
{
a[$1,2]+=$2;
##creating array a whose index is $1,2 where 2 denoted the 2nd field and it will add its all $2 values of whole Input_file.
a[$1,3]+=$3;
##creating array a whose index is $1,3 where 3 denoted the 3rd field and it will add its all $3 values of whole Input_file.
a[$1,4]+=$4;
##creating array a whose index is $1,4 where 4 denoted the 4th field and it will add its all $4 values of whole Input_file.
c[$1,2]++;
##creating array named c with index of $1,2 and incrementing its value each time to make sure no empty column values will come.
c[$1,3]++;
##creating array named c with index of $1,3 and incrementing its value each time to make sure no empty column values will come.
c[$1,4]++;
##creating array named c with index of $1,4 and incrementing its value each time to make sure no empty column values will come.
prev=$1
##Assigning variable prev value as column 1.
}
END{
for(i in a){
##Again traversing through the array a and getting the MEAN/AVG of last line which will not come before END block of awk so same logic above mentioned to get first field and its means of $2,$3 and $4.
split(i, b," ");
val[b[1]]=val[b[1]]?val[b[1]] FS sprintf("%0.2f",a[i]/c[i]):sprintf("%0.2f",a[i]/c[i]);
};
delete a;
print b[1],val[b[1]]
##printing value of array b with index 1 and array val whose index is value of array b[1] value.
}
' SUBSEP=" " file17
##Setting SUBSEP as space and Mentioning Input_file name above.
令人驚歎.....從來沒有聽說過這種工具。是否有一種方法來保持與輸入相同的小數位數。我不介意額外的零 – user2650277
@ user2650277,不,它執行計算並打印全實數 – RomanPerekhrest
或者'datamash -st,-g1表示2-4
randomir