Traitement de plusieurs fichier avec awk

Résolu
GHISLINO Messages postés 214 Date d'inscription   Statut Membre Dernière intervention   -  
dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   -
Bonjour,

j'ai 543 fichiers se presentant comme l'exemple ci- dessous

File
ISSIAQUATRE --> xxx.xx.xxx.xxx
spawn telnet xxx.xx.xxx.xxx
Trying xxx.xx.xxx.xxx...
Connected to xxx.xx.xxx.xxx.
Escape character is '^]'.
xxx.xxx.xxx.xxx login: xxxx
Password:

xxxxxxxxxxb> xxxx
/$ pltf_653/
/pltf_653$ pltf/mda_sfp/sfp_type 0
SFP_TYPE(0x2) is :1000 BASE-LX
/pltf_653$ pltf/mda_sfp/sfp_type 1
SFP_TYPE(0x0) is :
/pltf_653$ exit
End of session
/pltf_653$ Disconnection from 'launcher_571'
Disconnection from 'pltf_653'
Setting current level to root
xxxxxxxxxxb> Connection to xxx.xx.xxx.xxx. closed by foreign host.

je veux récuperer des informations dans ce fichier comme l'exemple ci-dessous

Fichier de sorti

ISSIAQUATRE; SFP_TYPE(0x2) is :1000 BASE-LX

si le champ SFP_TYPE n'est renseigné donner la sortie ci-dessous

ISSIAQUATRE; SFP_TYPE(0x2) is : Inconnu

merci d'avance

12 réponses

  1. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    hello
    essayer
    awk 'NR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); if(!a[2])a[2]="Inconnu"; print n, a[1], a[2]; nextfile}' fichier*
    1
  2. GHISLINO Messages postés 214 Date d'inscription   Statut Membre Dernière intervention  
     
    bonjour dubcek

    j 'ai l'erreur ci dessous à l'exécution

     awk 'NR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); if()a[2]="Inconnu"; print n, a[1], a[2]; nextfile}' BARADJI
    awk: syntax error near line 1
    awk: illegal statement near line 1 


    et avec nawk j ais ceci

    nawk 'NR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); if()a[2]="Inconnu"; print n, a[1], a[2]; nextfile}' BARADJI
    nawk: syntax error at source line 1
     context is
            NR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); >>>  if() <<<
    nawk: illegal statement at source line 1
    


    cdlt
    0
  3. GHISLINO Messages postés 214 Date d'inscription   Statut Membre Dernière intervention  
     
    bonjour

    à partir du file1 est t'il possible d'avoir le file2 : je pense que cela peut résoudre mon problème.

    Merci d'avance

    File1

    147_LOGEMENT
    SFP_TYPE(0x2) is :1000 BASE-LX
    ABADJIN
    ABATA
    SFP_TYPE(0x2) is :1000 BASE-LX
    ABATTOIR
    SFP_TYPE(0x2) is :1000 BASE-LX
    ABATTOIR_IHS
    ABEBROUKOI
    ABENGOUROU
    ABENGOUROU_CIT
    ABIA_ABETI
    SFP_TYPE(0x2) is :1000 BASE-LX
    ABOBO
    ABOBO_BAOULE
    ABOBO_MAITE
    ABOBO_ND

    obtenir File2 comme ceci

    147_LOGEMENT;SFP_TYPE(0x2) is :1000 BASE-LX
    ABADJIN
    ABATA;SFP_TYPE(0x2) is :1000 BASE-LX
    ABATTOIR;SFP_TYPE(0x2) is :1000 BASE-LX
    ABATTOIR_IHS
    ABEBROUKOI
    ABENGOUROU
    ABENGOUROU_CIT
    ABIA_ABETI;SFP_TYPE(0x2) is :1000 BASE-LX
    ABOBO
    ABOBO_BAOULE
    ABOBO_MAITE
    ABOBO_ND
    0
  4. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    remplacer NR==1 par FNR==1
    les ifs sont bizarres : if()a[2]="
    sur quel système ?
    0
    1. GHISLINO Messages postés 214 Date d'inscription   Statut Membre Dernière intervention  
       
      bonjour dubcek

      je suis sur Solaris 10

      cdlt
      0
    2. GHISLINO Messages postés 214 Date d'inscription   Statut Membre Dernière intervention  
       
      bonjour Dubcek

      j'ai remplacer le NR par FNR ci-dessous les erreurs

      awk 'FNR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); if()a[2]="Inconnu"; print n, a[1], a[2]; nextfile}' BARADJI
      awk: syntax error near line 1
      awk: illegal statement near line 1
      # nawk 'FNR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); if()a[2]="Inconnu"; print n, a[1], a[2]; nextfile}' BARADJI
      nawk: syntax error at source line 1
      context is
      FNR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); >>> if() <<<
      nawk: illegal statement at source line 1
      0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
     
    Salut,

    $ cat plop 
    147_LOGEMENT
    SFP_TYPE(0x2) is :1000 BASE-LX
    ABADJIN
    ABATA
    SFP_TYPE(0x2) is :1000 BASE-LX
    ABATTOIR
    SFP_TYPE(0x2) is :1000 BASE-LX
    ABATTOIR_IHS
    ABEBROUKOI
    ABENGOUROU
    ABENGOUROU_CIT
    ABIA_ABETI
    SFP_TYPE(0x2) is :1000 BASE-LX
    ABOBO
    ABOBO_BAOULE
    ABOBO_MAITE
    ABOBO_ND

    $ sed -n '$p;N;/\nSFP_/{s/\n/;/};P;D' plop
    147_LOGEMENT;SFP_TYPE(0x2) is :1000 BASE-LX
    ABADJIN
    ABATA;SFP_TYPE(0x2) is :1000 BASE-LX
    ABATTOIR;SFP_TYPE(0x2) is :1000 BASE-LX
    ABATTOIR_IHS
    ABEBROUKOI
    ABENGOUROU
    ABENGOUROU_CIT
    ABIA_ABETI;SFP_TYPE(0x2) is :1000 BASE-LX
    ABOBO
    ABOBO_BAOULE
    ABOBO_MAITE
    ABOBO_ND

    0
  7. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
     
    Re-

    Pour ton 1er problème, essaye ça :

    $ cat brol 
    ISSIAQUATRE --> xxx.xx.xxx.xxx
    spawn telnet xxx.xx.xxx.xxx
    Trying xxx.xx.xxx.xxx...
    Connected to xxx.xx.xxx.xxx.
    Escape character is '^]'.
    xxx.xxx.xxx.xxx login: xxxx
    Password:

    xxxxxxxxxxb> xxxx
    /$ pltf_653/
    /pltf_653$ pltf/mda_sfp/sfp_type 0
    SFP_TYPE(0x2) is :1000 BASE-LX
    /pltf_653$ pltf/mda_sfp/sfp_type 1
    SFP_TYPE(0x0) is :
    /pltf_653$ exit
    End of session
    /pltf_653$ Disconnection from 'launcher_571'
    Disconnection from 'pltf_653'
    Setting current level to root
    xxxxxxxxxxb> Connection to xxx.xx.xxx.xxx. closed by foreign host.

    $ sed -n '1{s/ .*//;h};/^SFP_/{/:$/{s/.*/& Inconnu/};G;s/\([^\n]*\)\n\(.*\)/\2;\1/p}' brol
    ISSIAQUATRE;SFP_TYPE(0x2) is :1000 BASE-LX
    ISSIAQUATRE;SFP_TYPE(0x0) is : Inconnu


    Pour plusieurs fichiers à traiter écrire cette syntaxe :
    sed -s -n '<syntaxe>' fich*

    Zen my nuggets ;-)
    Faites un geste pour l'environnement, fermez vos fenêtres et adoptez un manchot. <('')
    0
  8. GHISLINO Messages postés 214 Date d'inscription   Statut Membre Dernière intervention  
     
    bonjour Zipe31

    Merci pour tes différents retour : ci-dessous mes messages d'erreurs

    NB: je suis sur solaris 10

    premier cas :

    % sed -n '$p;N;/\nSFP_/{s/\n/;/};P;D' NODEB_SFP.xls
    sed: command garbled: $p;N;/\nSFP_/{s/\n/;/};P;D
    

    Deuxième cas

    % sed -n '1{s/ .*//;h};/^SFP_/{/:$/{s/.*/& Inconnu/};G;s/\([^\n]*\)\n\(.*\)/\2;\1/p}' KAHIRA
    sed: command garbled: 1{s/ .*//;h};/^SFP_/{/:$/{s/.*/& Inconnu/};G;s/\([^\n]*\)\n\(.*\)/\2;\1/p}


    en attente de ton retour

    cdlt.
    0
    1. zipe31 Messages postés 34620 Date d'inscription   Statut Contributeur Dernière intervention   6 501
       
      Essaie en créant des fichiers de script (mettre les instructions dans un fichier) :

      $ cat script.sed 
      #n
      $ p
      N
      /\nSFP_/ {
      s/\n/;/
      }
      P
      D

      $ sed -f script.sed plop
      147_LOGEMENT;SFP_TYPE(0x2) is :1000 BASE-LX
      ABADJIN
      ABATA;SFP_TYPE(0x2) is :1000 BASE-LX
      ABATTOIR;SFP_TYPE(0x2) is :1000 BASE-LX
      ABATTOIR_IHS
      ABEBROUKOI
      ABENGOUROU
      ABENGOUROU_CIT
      ABIA_ABETI;SFP_TYPE(0x2) is :1000 BASE-LX
      ABOBO
      ABOBO_BAOULE
      ABOBO_MAITE
      ABOBO_N

      Idem pour le second :

      $ cat script2.sed 
      #n
      1 {
      s/ .*//
      h
      }
      /^SFP_/ {
      /:$/ {
      s/.*/& Inconnu/
      }
      G
      s/\([^\n]*\)\n\(.*\)/\2;\1/p
      }

      $ sed -s -f script2.sed b*
      AAAABBBB;SFP_TYPE(0x2) is :1000 BASE-LX
      AAAABBBB;SFP_TYPE(0x0) is : Inconnu
      ISSIAQUATRE;SFP_TYPE(0x2) is :1000 BASE-LX
      ISSIAQUATRE;SFP_TYPE(0x0) is : Inconnu
      0
  9. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    nawk Solaris ne connait pas nextfile
    essayer de l'enlever
    0
  10. GHISLINO Messages postés 214 Date d'inscription   Statut Membre Dernière intervention  
     
    bonjour dubcek

    ci-dessous le résultat sans nextfile

     nawk 'FNR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); if()a[2]="Inconnu"; print n, a[1], a[2]}' BARADJI
    nawk: syntax error at source line 1
     context is
            FNR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); >>>  if() <<<
    nawk: illegal statement at source line 1

    # awk 'FNR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); if()a[2]="Inconnu"; print n, a[1], a[2]}' BARADJI
    awk: syntax error near line 1
    awk: illegal statement near line 1
    
    0
    1. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
       
      pourquoi le if() est vide ? if(!a[2])a[2]="Inconnu";
      0
  11. GHISLINO Messages postés 214 Date d'inscription   Statut Membre Dernière intervention  
     
    bonjour dubcek

    j'ai repris le code et ci-dessous les resultats

    awk 'NR==1 {n=$1} /SFP_TYPE\(0x2\)/ {split($0, a, ":"); if(!a[2])a[2]="Inconnu"; print n, a[1], a[2]}' BARADJI
    BARADJI         SFP_TYPE(0x2) is  1000 BASE-LX


    mais pense que j'ai un petit soucis

    comme je l'ai dis plus haut j'ai 543 fichiers à traiter et il faut que j'adapte le code à ces différents fichiers ci-dessous les différents types de fichier possible:

    les differences sont comme suite

    Type1 :
    caractérisé par cette ligne SFP_TYPE(0x2) is :1000 BASE-LX

    Type2:
    caractérisé par cette ligne SFP_TYPE(0x0) is :1000 BASE-LX

    Type 3:

    le type de SFP_type n'est pas connu et il faut mettre Inconnu.

    Type1
    ISSIAQUATRE --> xxx.xx.xxx.xxx
    spawn telnet xxx.xx.xxx.xxx
    Trying xxx.xx.xxx.xxx...
    Connected to xxx.xx.xxx.xxx.
    Escape character is '^]'.
    xxx.xxx.xxx.xxx login: xxxx
    Password:
    
                                                                          
    
    xxxxxxxxxxb> xxxx
    /$ pltf_653/
    /pltf_653$ pltf/mda_sfp/sfp_type 0
            SFP_TYPE(0x2) is :1000 BASE-LX
    /pltf_653$ pltf/mda_sfp/sfp_type 1
            SFP_TYPE(0x0) is :
    /pltf_653$ exit
    End of session
    /pltf_653$ Disconnection from 'launcher_571'
    Disconnection from 'pltf_653'
    Setting current level to root
    xxxxxxxxxxb> Connection to xxx.xx.xxx.xxx. closed by foreign host.


    Type2
    MAMADI  --> xxx.xx.xxx.xxx
    spawn telnet xxx.xx.xxx.xxx
    Trying xxx.xx.xxx.xxx...
    Connected to xxx.xx.xxx.xxx.
    Escape character is '^]'.
    xxx.xxx.xxx.xxx login: xxxx
    Password:
    
                                                                   
           
    xxxxxxxxxxb> xxxx
    /$ pltf_653/
    /pltf_653$ pltf/mda_sfp/sfp_type 0
            SFP_TYPE(0x2) is :
    /pltf_653$ pltf/mda_sfp/sfp_type 1
            SFP_TYPE(0x0) is :1000 BASE-LX
    /pltf_653$ exit
    End of session
    /pltf_653$ Disconnection from 'launcher_571'
    Disconnection from 'pltf_653'
    Setting current level to root
    xxxxxxxxxxb> Connection to xxx.xx.xxx.xxx. closed by foreign host.


    type 3

    LUMIERE --> xxx.xx.xxx.xxx
    spawn telnet xxx.xx.xxx.xxx
    Trying xxx.xx.xxx.xxx...
    Connected to xxx.xx.xxx.xxx.
    Escape character is '^]'.
    xxx.xxx.xxx.xxx login: xxxx
    Password:
    
    
    xxxxxxxxxxb> bcish
    /$ pltf_505/
    /pltf_505$ pltf/mda_sfp/sfp_type 0
    Unable to get the remote server name as the pathname 'pltf/mda_sfp/sfp_type' is not absolute
    /pltf_505$ pltf/mda_sfp/sfp_type 1
    Unable to get the remote server name as the pathname 'pltf/mda_sfp/sfp_type' is not absolute
    /pltf_505$ exit
    End of session
    /pltf_505$ Disconnection from 'launcher_436'
    Disconnection from 'pltf_505'
    Setting current level to root
    0
  12. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    c'est plus simple de traiter un fichier à la fois, donc :
    $ for F in fichier*; do nawk ' FNR==1 {n=$1; x=0} /SFP_TYPE/ && /BASE/ {x++; $1=$1; print n "\t" $0} END {if(!x)print n "\tSFP_TYPE : Inconnu"}' $F; done
    ISSIAQUATRE SFP_TYPE(0x2) is :1000 BASE-LX
    MAMADI SFP_TYPE(0x0) is :1000 BASE-LX
    LUMIERE SFP_TYPE : Inconnu
    $
    0
  13. dubcek Messages postés 18627 Date d'inscription   Statut Contributeur Dernière intervention   5 660
     
    sans boucle
    $ nawk 'func a() {if(!x)s="\tSFP_TYPE : Inconnu"; print n, s} FNR<2 {if(p++)a(); n=$1; x=0} /SFP_TYPE/ && /BASE/ {x++; $1=$1; s=$0} END {a()}' fichier*
    ISSIAQUATRE SFP_TYPE(0x2) is :1000 BASE-LX
    MAMADI SFP_TYPE(0x0) is :1000 BASE-LX
    LUMIERE SFP_TYPE : Inconnu
    0