Domanda

I have writen this piece of code

for fichier in $(find $DATA $ARCH -type f -mtime $histo)
do
    compteur_lignes=$(cat $fichier|wc -l)
    echo "Nombre de lignes : $compteur_lignes"
      quantieme_f=$(date -r $fichier +%j)
    echo "Quantieme fichier : $quantieme_f"
    nom_fichier=$(basename $fichier)
    echo $nom_fichier
    awk -vquant_f=$quantieme_f -vtype_f='' -vNB_LG=$compteur_lignes -vnom_fic=$nom_fichier '{ if ( $1 == 01 ) {type_f=substr($0,21,7);printf "\n - Type %s, Quantième du jour de réception %s, Nombre de lignes du fichier %s, Nom du fichier %s\n" , type_f, quant_f, NB_LG, nom_fic}}' ${fichier} >> ${MAIL_CORP}
done

And the output is something Like That :

 - Type TYPEN1, Quantième du jour de réception 126, Nombre de lignes du fichier 6, Nom du fichier Test.txt

I need to make a join with an delimited file on the first column :

TYPEN1:TRANSCON1
TYPEN2:TRANSCON2
TYPEN3:TRANSCON3
TYPEN4:TRANSCON4

To finally have this output :

 - Type TYPEN1 (TRANSCON1), Quantième du jour de réception 126, Nombre de lignes du fichier 6, Nom du fichier Test.txt

How can I do that ?

È stato utile?

Soluzione

This awk can work for you:

awk 'FNR==NR{if (split($0,t,":")) a[t[1]]=t[2]; next} {for (i=1; i<=NF; i++) 
     {f=$i; gsub(/,/, "", f); if (f in a) $i=f " (" a[f] "),"} print }' f1 f2
- Type TYPEN1 (TRANSCON1), Quantième du jour de réception 126, Nombre de lignes du fichier 6, Nom du fichier Test.txt

Altri suggerimenti

Assuming that the output of your loop is a and the file is b, this would

awk -F: 'NR==FNR { split($0,a,","); next }
         { 
           s= a[1] " (" $2 ")"
           for(i=2; i<=length(a); ++i) s=s "," a[i]
           print s
         }' a b

This processes the two inputs one line at a time. The first block is executed on the first input (because NR==FNR) and the next skips to the next input. The second block sandwiches the part from the second input into the first input.

output:

 - Type TYPEN1 (TRANSCON1), Quantième du jour de réception 126, Nombre de lignes du fichier 6, Nom du fichier Test.txt

There is an assumption made here, that the lines from both inputs will be in the correct order (easy to fix using sort) and there won't be any lines missing. Let me know if that's a problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top