Decompte de module et Somme avec AWK

Résolu/Fermé
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023 - 27 janv. 2016 à 10:39
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023 - 29 janv. 2016 à 14:45
Bonjour,

J'ai un fichier file1 ci-dessous je veux obtenir le file2 avec AWK
c'est à dire un décompte des modules contenues dans le file1 et faire la somme par type de modules
merci d'avance

File1

BSC1;Ministere-GD-OA_LIT_064;RMXA18
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;RMXA09
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;RMXA18
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;RMXA18
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;RMXA09
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;RMXA09
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;RMXA09FW
BSC1;Ministere-GD-OA_LIT_064;RMXA09FW
BSC1;Ministere-GD-OA_LIT_064;RMXA09FW
BSC1;Ministere-GD-OA_LIT_064;RMXA18FW
BSC1;Ministere-GD-OA_LIT_064;RMXA18FW
BSC1;Ministere-GD-OA_LIT_064;RMXA18FW
BSC1;Ministere-GD-OA_LIT_064;SUXGNC
BSC1;Ministere-GD-OA_LIT_064;SUMX
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;SFP
BSC1;Ministere-GD-OA_LIT_064;CI1SU
BSC2;Lomie_Idm-OS_EST_010;MBI5
BSC2;Lomie_Idm-OS_EST_010;FACB
BSC2;Lomie_Idm-OS_EST_010;TGT09
BSC2;Lomie_Idm-OS_EST_010;TRAG
BSC2;Lomie_Idm-OS_EST_010;TRAG
BSC2;Lomie_Idm-OS_EST_010;TAGHE
BSC2;Lomie_Idm-OS_EST_010;SUMA
BSC2;Lomie_Idm-OS_EST_010;ANCG
BSC2;Lomie_Idm-OS_EST_010;FACB
BSC2;Lomie_Idm-OS_EST_010;TRAG
BSC2;Lomie_Idm-OS_EST_010;TAGHE
BSC2;Lomie_Idm-OS_EST_010;TAGHE
BSC2;Lomie_Idm-OS_EST_010;TAGHE
BSC2;Lomie_Idm-OS_EST_010;ANCG
BSC2;Lomie_Idm-OS_EST_010;ANCG
BSC2;Lomie_Idm-OS_EST_010;FACB
BSC2;Lomie_Idm-OS_EST_010;TRAGE
BSC2;Lomie_Idm-OS_EST_010;TGT09
BSC2;Lomie_Idm-OS_EST_010;XIBM
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD
BSC2;Lomie_Idm-OS_EST_010;ADDIMOD


File2

BSC ; BTS ;RMXA18;SFP;RMXA09;RMXA09FW;RMXA18FW;SUXGNC;SUMX;CI1SU;MBI5;FACB;TGT09;TRAG;TAGHE;SUMA;ANCG;TRAGE;XIBM;ADDIMOD;
BSC1;Ministere-GD-OA_LIT_064; 3 ; 12; 3 ; 3 ; 3 ; 1 ; 1 ; 1 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ;
BSC2;Lomie_Idm-OS_EST_010; 0 ; 0 ; 0 ; 0 ; 0 ; 0 ; 0 0 ; 1 ; 3 ; 2 ; 3 ; 4 ; 1 ; 3 ; 1 ; 1 ; 15 ;
TOTAL ; 3 ; 12; 3 ; 3 ; 3 ; 1 ; 1 ; 1 ; 1 ; 3 ; 2 3 ; 4 ; 1 ; 3 ; 1 ; 1 ; 15 ;


Cdlt.

8 réponses

mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 7 749
27 janv. 2016 à 10:55
Bonjour,

J'espère que le but n'est pas de faire ton travail à ta place ? Parce que bon CCM est un forum d'entre-aide, donc si tu es bloqué sur un point précis, on est là pour t'aider. Sous-traiter... c'est autre chose :-)

Tu pourrais par exemple nous dire ce que tu as commencé à faire, ce qui te bloque, etc.

Ah et accessoirement, la manière dont ton fichier2 est incompréhensible sans explication, donc même si on voulait t'aider, on passerait déjà un temps non négligeable à comprendre ce que tu cherches à faire.

Cordialement
0
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
27 janv. 2016 à 14:19
bonjour Mamiemando

merci pour ton feedback , effet si j'ai posté ce thread ce n'est pas pour que vous fassiez mon travail à ma place, non loin de la .

cette tâche que je viens de posté je le fais bien des boucle for et et des grep et wc-l cela fait un long code.

or je sais que pour ceux qui maitrise AWK cela sera fera juste sur une seule ligne.
donc c'est l'expereience de AWk que moi je recherche.

Merci pour ta compréhension.

ci dessous le File 2


BSC;BTS;RMXA18;SFP;RMXA09;RMXA09FW;RMXA18FW;SUXGNC;SUMX;CI1SU;MBI5;FACB;TGT09;TRAG;TAGHE;SUMA;ANCG;TRAGE;XIBM;ADDIMOD;
BSC1;Ministere-GD-OA_LIT_064;3;12;3;3;3;1;1;1;0;0;0;0;0;0;0;0;0;0;
BSC2;Lomie_Idm-OS_EST_010;0;0;0;0;0;0;00;1;3;2;3;4;1;3;1;1;15;
TOTAL;3;12;3;3;3;1;1;1;1;3;23;4;1;3;1;1;15;


0
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
28 janv. 2016 à 10:03
hello
l'ordre des colonnes est important ?
0
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
28 janv. 2016 à 10:09
Bonjour Dubcek

Non pas important , mais le décompte doit juste matcher avec les items

RMXA18;SFP;RMXA09;RMXA09FW;RMXA18FW;





cdlt.
0
dna.factory Messages postés 24957 Date d'inscription mercredi 18 avril 2007 Statut Modérateur Dernière intervention 26 avril 2024 1 609
28 janv. 2016 à 11:15
Si j'ai bien compris l'objectif, je pense que cette page (et evidemment les deux pages aboutissant à celle là) pourrait t'aider :
https://gentoo-handbook.lugons.org/doc/en/articles/l-awk3.xml
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
mamiemando Messages postés 33079 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 23 avril 2024 7 749
28 janv. 2016 à 11:32
Un premier essai (qui ne fait pas ce qu'il faut) mais qui te donnera un point de départ.

#!/usr/bin/env awk

/.*;.*;.*/ {
split($0, a, ";");
i = a[1]
j = a[2]
k = a[3]
a[i][j][k] += 1
print i, j, k, a[i][j][k]
}

END {
for (i in a) {
for (j in a[i]) {
for (k in a[i][j]) {
print(i ";" j ";" k ";" a[i][j][k]);
}
}
}
}


Puis :

cat file | awk -f script.awk


Bonne chance
0
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
28 janv. 2016 à 13:32
Bonjour à tous


Merci pour votre feedback je testerai chacun de vos exemple.

cependant comme je l'avait dis mon code fonction bien mais le traitement est plus car j'ai plus de 10000 lignes à traiter.

ci-dessous mon code

#!/bin/bash
#
#
# Written by ghislain 25/01/2016

cat File_OMCR1.csv | cut -d';' -f2,3,11 >> RI_OMCR1.csv
cat RI_OMCR1.csv | cut -d';' -f3 | sort | uniq -d | grep -v '??????' >> MEMONIC.csv

for btsx in $(cat BTS_OMCR.csv); do
bts=$(echo $btsx|cut -d';' -f2)
BSC=$(echo $btsx|cut -d';' -f1)

cat RI_OMCR1.csv | grep -w $bts > $bts.csv

for Modx in $(cat MEMONIC.csv); do
Modx=$(echo $Modx |cut -d' ' -f1)

#echo $bts $Modx

R=`grep -w $Modx $bts.csv | wc -l`

echo $BSC";"$bts";"$R >> $bts.txt

mod=`cat $bts.txt | awk 'BEGIN { FS=";"; ORS=";"}{ print $3 }'`

echo $BSC";"$bts";"$mod > $bts"_1".csv


done
done
rm $bts.csv $bts.txt
0
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
28 janv. 2016 à 15:17
essayer (et vérifier)
$ cat f3.awk
{t1[$1, $2]++; t2[$3]++; t[$1, $2, $3]++}

END {
printf "BSC;BTS;"; for (n in t2)printf n FS
print ""
for (n in t1) {
n1=n; gsub(SUBSEP, FS, n1); printf n1 FS
for (n2 in t2)
if(x=t[n, n2])printf x FS ; else printf "0;"
print ""
}
printf "TOTAL;"; for (n in t2)printf t2[n] FS
print ""
}
$
$ awk -F ";" -f f3.awk fichier
BSC;BTS;ANCG;SUMA;ADDIMOD;MBI5;SUXGNC;TAGHE;CI1SU;SFP;XIBM;TRAGE;TRAG;FACB;RMXA18FW;SUMX;RMXA09;RMXA18;TGT09;RMXA09FW;
BSC2;Lomie_Idm-OS_EST_010;3;1;15;1;0;4;0;0;1;1;3;3;0;0;0;0;2;0;
BSC1;Ministere-GD-OA_LIT_064;0;0;0;0;1;0;1;12;0;0;0;0;3;1;3;3;0;3;
TOTAL;3;1;15;1;1;4;1;12;1;1;3;3;3;1;3;3;2;3;
0
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
29 janv. 2016 à 14:45
Bonjour à tous ,

Merci à tous pour votre contribution votre m'a baucoup aidez.

Dubcek ton code fonctionne tres bien j'ai juste apporté une correction en ajoutant un ";" sur la ligne de commande du TOTAL car le resultat etait decalé.

printf "TOTAL;";"; for (n in t2)printf t2[n] FS


petite remarque : mon script met environ 2 heure pour traiter le Fichier gobal de 10000 ligne

Par compte le code de Dubcek met 5 second pour le traitement , Chapeau

et merci encore à vous tous.

cordialement
0