AWK: additionner certains champs des lignes d'un fichier dont

Résolu/Fermé
bob737 Messages postés 144 Date d'inscription jeudi 24 septembre 2009 Statut Membre Dernière intervention 14 mars 2024 - Modifié par bob737 le 26/07/2016 à 15:47
bob737 Messages postés 144 Date d'inscription jeudi 24 septembre 2009 Statut Membre Dernière intervention 14 mars 2024 - 26 juil. 2016 à 18:00
le deuxième champ est identique.

Bonjour,

Exemple :
VAR1;TOTO;5;10;15
VAR1; TOTO;10;20;30

==> VAR1;TOTO;15;30;45

Merci. ;-)
A voir également:

2 réponses

bob737 Messages postés 144 Date d'inscription jeudi 24 septembre 2009 Statut Membre Dernière intervention 14 mars 2024
26 juil. 2016 à 16:07
awk 'BEGIN {FS=OFS=","} {a[$2]=a[$2]; SUM1+=$3; SUM2+=$4; SUM3+=$5} END { print SUM1, SUM2, SUM3}' test

j'avance mais ma condition ne marche pas....
0
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
26 juil. 2016 à 16:41
hello
$ cat fichier
VAR2;TATA;1;20;25
VAR2; TATA;10;20;30
VAR1;TOTO;5;10;15
VAR1; TOTO;10;20;30
$ awk -F " *; *" -v OFS=";" '{t[$2]=$1; t1[$2]+=$3; t2[$2]+=$4; t3[$2]+=$5} END {for(n in t)print t[n], n, t1[n], t2[n], t3[n]}' fichier
VAR2;TATA;11;40;55
VAR1;TOTO;15;30;45
0
bob737 Messages postés 144 Date d'inscription jeudi 24 septembre 2009 Statut Membre Dernière intervention 14 mars 2024
26 juil. 2016 à 17:19
cooool merci.

je n'ai pas d'espace dans mes champs et 8 champs sur chaque ligne soit
awk 'BEGIN {FS=OFS=","} {t[$2]=$1; t1[$2]+=$3; t2[$2]+=$4; t3[$2]+=$5; t4[$2]+=$6; t5[$2]+=$7; t6[$2]+=$8} END {for(n in t)print t[n], n, t1[n], t2[n], t3[n], t4[n], t5[n], t6[n]}' tmp_flux.csv

Par contre, l'addition de certaine valeur du champ 5 donne:
6.18188e+09 au lieu de 6181884397. Y a-t-il moyen de garder la mise en forme?
0
bob737 Messages postés 144 Date d'inscription jeudi 24 septembre 2009 Statut Membre Dernière intervention 14 mars 2024
26 juil. 2016 à 18:00
ça semble régler le pb:
awk 'BEGIN {FS=OFS=","; OFMT="%.13g"} {t[$2]=$1; t1[$2]+=$3; t2[$2]+=$4; t3[$2]+=$5; t4[$2]+=$6; t5[$2]+=$7; t6[$2]+=$8} END {for(n
in t)print t[n], n, t1[n], t2[n], t3[n], t4[n], t5[n], t6[n]}' tmp_flux.csv | sort -t,
0