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 18789 Date d'inscription Statut Contributeur Dernière intervention -
dubcek Messages postés 18789 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
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:
- Traitement de plusieurs fichier avec awk
- Fichier bin - Guide
- Fichier epub - Guide
- Fichier rar - Guide
- Comment réduire la taille d'un fichier - Guide
- Fichier .dat - Guide
12 réponses
hello
essayer
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*
bonjour dubcek
j 'ai l'erreur ci dessous à l'exécution
et avec nawk j ais ceci
cdlt
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
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
à 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
remplacer NR==1 par FNR==1
les ifs sont bizarres : if()a[2]="
sur quel système ?
les ifs sont bizarres : if()a[2]="
sur quel système ?
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
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
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
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
Re-
Pour ton 1er problème, essaye ça :
Pour plusieurs fichiers à traiter écrire cette syntaxe :
Zen my nuggets ;-)
Faites un geste pour l'environnement, fermez vos fenêtres et adoptez un manchot. <('')
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. <('')
bonjour Zipe31
Merci pour tes différents retour : ci-dessous mes messages d'erreurs
NB: je suis sur solaris 10
premier cas :
Deuxième cas
en attente de ton retour
cdlt.
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.
Essaie en créant des fichiers de script (mettre les instructions dans un fichier) :
Idem pour le second :
$ 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
bonjour dubcek
ci-dessous le résultat sans nextfile
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
bonjour dubcek
j'ai repris le code et ci-dessous les resultats
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
Type2
type 3
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
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
$