Traitement de plusieurs fichier 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 - 11 mai 2014 à 11:52
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 - 13 mai 2014 à 09:00
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








A voir également:

12 réponses

dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
11 mai 2014 à 16:20
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
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
12 mai 2014 à 07:05
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
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
12 mai 2014 à 08:22
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
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
Modifié par dubcek le 12/05/2014 à 09:06
remplacer NR==1 par FNR==1
les ifs sont bizarres : if()a[2]="
sur quel système ?
0
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
12 mai 2014 à 10:47
bonjour dubcek

je suis sur Solaris 10

cdlt
0
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
12 mai 2014 à 10:52
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

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

Posez votre question
zipe31 Messages postés 36402 Date d'inscription dimanche 7 novembre 2010 Statut Contributeur Dernière intervention 27 janvier 2021 6 407
12 mai 2014 à 08:38
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
zipe31 Messages postés 36402 Date d'inscription dimanche 7 novembre 2010 Statut Contributeur Dernière intervention 27 janvier 2021 6 407
Modifié par zipe31 le 12/05/2014 à 09:03
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
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
12 mai 2014 à 10:11
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
zipe31 Messages postés 36402 Date d'inscription dimanche 7 novembre 2010 Statut Contributeur Dernière intervention 27 janvier 2021 6 407
12 mai 2014 à 12:33
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
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
12 mai 2014 à 10:45
nawk Solaris ne connait pas nextfile
essayer de l'enlever
0
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
12 mai 2014 à 10:56
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
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
12 mai 2014 à 11:42
pourquoi le if() est vide ? if(!a[2])a[2]="Inconnu";
0
GHISLINO Messages postés 214 Date d'inscription lundi 25 juillet 2011 Statut Membre Dernière intervention 8 décembre 2023
12 mai 2014 à 12:39
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
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
12 mai 2014 à 15:21
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
dubcek Messages postés 18718 Date d'inscription lundi 15 janvier 2007 Statut Contributeur Dernière intervention 22 mars 2024 5 615
13 mai 2014 à 09:00
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