Shell - Parcours de fichiers imbriqués

Bonjour,

Je souhaite ecrire un script qui parcoure un fichier pour en mettre un autre à jour en fonction d'un champ

Fichier PARAMETRES (ID_PARAM;CHAMP1;CHAMP2;CHAMP3) environ 5 lignes
1;A;B;B
2;C;-;G
3;B;D;E
Fichier CLIENTS (NOM;PRENOM;ID_PARAM) environ 1000 lignes
DUPONT;JACQUES;1
DURAND;PIERRE;2
MARTIN;JACQUES;3
ANDRE;GILLES;2
....

et je souhaiterais avoir la sortie suivante :
DUPONT;JACQUES;1;A;B;B
DURAND;PIERRE;2;C;-;G
MARTIN;JACQUES;3;B;D;E
ANDRE;GILLES;2;C;-;G
....

Je pensais imbriquer un awk dans un while, mais je m'y perd un peux...
Quelqu'un aurait t'il une idée ?

Merci d'avance.
______________________________________________

Désolé, j'ai trouvé la solution avec un simple join.
______________________________________________

Dipsou

6 réponses

  • Votes
  • Date
  • Date décroissante
  1. Contributeur
    hello
    $ awk 'BEGIN{FS=OFS=";"} NR==FNR {t[$1]=$0; next} {$3=t[$3]; print }' paramètres clients
    DUPONT;JACQUES;1;A;B;B
    DURAND;PIERRE;2;C;-;G
    MARTIN;JACQUES;3;B;D;E
    ANDRE;GILLES;2;C;-;G
    $ 
    
    1. Modérateur
      La commande join est destinée à ce genre d'opération:
      johand@osiris: ~/src/CCM/bash/join $ cat PARAMETRES    
      1;A;B;B    
      2;C;-;G    
      3;B;D;E    
      johand@osiris: ~/src/CCM/bash/join $ cat CLIENTS     
      DUPONT;JACQUES;1    
      DURAND;PIERRE;2    
      MARTIN;JACQUES;3    
      ANDRE;GILLES;2    
      johand@osiris: ~/src/CCM/bash/join $ cat doit     
      join -t ';' -1 1 -2 3  -o "2.1 2.2 1.1 1.2 1.3 1.4" PARAMETRES CLIENTS    
      johand@osiris: ~/src/CCM/bash/join $ source doit     
      DUPONT;JACQUES;1;A;B;B    
      DURAND;PIERRE;2;C;-;G    
      MARTIN;JACQUES;3;B;D;E


      man join; info join

      <edit> La ligne ANDRE;GILLES n'apparaît pas. Je cherche la solution </edit>
      Il faut au minimum trier CLIENTS selon le champ commun:
      sort -t ";" -k 3 CLIENTS > CLIENTS.sorted et travailler avec le fichier trié.

      Gates gave ^H sold you the windows.
      GNU gave us the whole house.(Alexandrin)
      1. Merci à vous deux. Super efficace.
        Apparement, ça marche avec le join.
        Je n'ai pas testé si je n'ai pas d'erreurs.
        Sinon, je suivrais les conseils de Jisisiv

        En tout cas, je garde au chaud ta réponse dubcek.
        1. J'ai effectivement des lignes qui manquent.
          En triant le fichier client, toujours le problème
          En triant le fichier paramètres et le fichier client, le problème est résolu !!!!

          Merci encore.
          1. Contributeur
            il faut trier avec join
            man join
            ... 
            Important: FILE1 and FILE2 must be sorted on the join fields.
            ...