awk pour sauver (en considérant que votre mode est triée INPUT_FILE, sinon vous pouvez utiliser sort -t, -k1 | awk ...
avant le code suivant aussi):
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
sortie sera comme suit.
0.5 4.18,0.77,0.01
0.6 3.74,0.77,0.01
0.7 3.84,0.78,0.02
Ajout maintenant d'une forme de doublure non-one.
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
EDIT: Ajout d'explication commande trop maintenant.
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.
Incroyable ..... jamais entendu parler de cet utilitaire.Est-il un moyen de garder le même nombre de décimales que dans l'entrée.Je ne dérange pas les zéros supplémentaires – user2650277
@ user2650277, non, il effectue des calculs et imprime la nombres réels entiers – RomanPerekhrest
Ou, 'datamash -st, -g1 signifie 2-4
randomir