Précédent   Forum des professionnels en informatique > Systèmes > Linux > Applications > Shell
Shell Vos questions sur l'utilisation des commandes shell
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 29/12/2007, 14h39   #1
Membre du Club
 
Inscription : mars 2006
Messages : 158
Détails du profil
Informations forums :
Inscription : mars 2006
Messages : 158
Points : 48
Points : 48
Par défaut awk et séparateur caractères spéciaux

Salut,

à partir d'un fichier 1.txt contenant ce genre de lignes

Code :
1
2
3
4
5
expression1 pouvant contenir des caractères spéciaux C:\windows\abc.txt
expression2 pouvant contenir des caractères spéciaux C:\windows\system32\z.dll
expression3 pouvant contenir des caractères spéciaux F:\zeighnzcjrt.sys
expression4 pouvant contenir des caractères spéciaux C:\a.inf
etc
je souhaite isoler dans un fichier 2.txt les chemins, càd obtenir ce résultat

Code :
1
2
3
4
C:\windows\abc.txt
C:\windows\system32\z.dll
F:\zeighnzcjrt.sys
C:\a.inf
L'algorithme serait - à mon sens - le suivant :

Citation:
pour chaque ligne du fichier 0.txt -> extraire toutes les chaîne de caractères qui commencent par l'expression : [a-zA-Z]:\
J'ai essayé un certain nombre de commandes et il me semble que awk est la plus appropriée. En m'inspirant des quelques pages web relatives à ce genre de pb, j''en suis arrivé à ceci :

awk -F.*: '{print ".*:" $2 }' 1.txt

qui donne un résultat presque potable mais loin des attentes :

Code :
1
2
3
4
.*:\windows\abc.txt
.*:\windows\system32\z.dll
.*:\zeighnzcjrt.sys
.*:\a.inf
Icomplet et inabouti car je souhaiterais que la lettre de lecteur apparaisse (C, F, etc) et d'autre part, je souhaite définir le séparateur le plus précisément possible, càd un truc du genre :

awk -F[a-zA-Z]:\\ '{print......}

Malheureusement la plupart des combinaisons testées avec ce séparateur n'aboutissent à rien d'exploitable ou à des erreurs de syntaxe

Est-ce que awk est selon vous la bonne direction ? comment utiliser correctement ce séparateur ?

Merci
eZula est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 29/12/2007, 23h26   #2
Membre expérimenté
 
Avatar de BlaireauOne
 
Inscription : mars 2007
Messages : 469
Détails du profil
Informations personnelles :
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations forums :
Inscription : mars 2007
Messages : 469
Points : 576
Points : 576
http://www.shellunix.com/awk.html
http://pagesperso-orange.fr/gleu/abs...fr/x15822.html


Code :
1
2
3
4
5
echo "* Solution avec sed : "
sed -n '/ [a-zA-Z]:\\/s/^.* \([a-zA-Z]:\\.*$\)/\1/p' liste.txt
 
echo -e "\n\n* Solution avec awk : "
awk '$NF ~ /[a-zA-Z]:\\/ {print $NF}' liste.txt

Code :
1
2
3
4
5
6
7
8
9
10
11
12
* Solution avec sed : 
C:\windows\abc.txt
C:\windows\system32\z.dll
F:\zeighnzcjrt.sys
C:\a.inf
 
 
* Solution avec awk : 
C:\windows\abc.txt
C:\windows\system32\z.dll
F:\zeighnzcjrt.sys
C:\a.inf
__________________
Loi de Murphy:
La Théorie c'est quand ça ne marche pas mais que l'on sait pourquoi.
La Pratique c'est quand ça marche mais qu'on ne sait pas pourquoi.
Quand la théorie rejoint la pratique ça ne marche pas et on ne sait pas pourquoi.
BlaireauOne est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 30/12/2007, 18h48   #3
Membre du Club
 
Inscription : mars 2006
Messages : 158
Détails du profil
Informations forums :
Inscription : mars 2006
Messages : 158
Points : 48
Points : 48
Merci BlaireauOne Ca fonctionne bien (sed donne un meilleur résultat que awk), sauf qu'en comparant le fichier initial avec le final, 36 lignes sont manquantes (1443 sur 1479 attendues) :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
O4 - HKLM\..\Run: [Automatic Media Update] "C:\WINDOWS\SYSTEM32\SUPPRT.RVD" -a
F2 - REG:system.ini: UserInit=userinit.exe,C:\WINDOWS\system\lsass.exe
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\explorer.exe
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ldr.exe
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ndetect.exe,
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ntss.exe
O4 - HKLM\..\Run: [Network Translation System Service] "C:\WINDOWS\system32\ntss.exe" * 
F2 - REG:system.ini: UserInit=C:\WINDOWS\System32\userinit.exe,C:\WINDOWS\System32\wfvs.exe
O4 - HKLM\..\Run: [Windows File Verification Service] "C:\WINDOWS\System32\wfvs.exe" *
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wsnpoem.exe,
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\codeblocks.exe,
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wscvs.exe 
O4 - HKLM\..\Run: [Windows Server Client Verification Service] "C:\WINDOWS\system32\wscvs.exe" * 
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,ntsvc32.dll
O4 - HKLM\..\Run: [Windows Services] "C:\Program Files\svchosts.exe"
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wspvs.exe
O4 - HKLM\..\Run: [Windows Server Peer Verification Service] "C:\WINDOWS\system32\wspvs.exe" * 
F3 - REG:win.ini: run=C:\WINDOWS\ServicePackFiles\services.exe
F3 - REG:win.ini: run=C:\WINDOWS\ServicePackFiles\winlogon.exe
O23 - Service: AOL Smart Update Service (AOL_Updater) - Unknown owner - "C:\WINDOWS\Help\aolupd.exe"
F3 - REG:win.ini: load=C:\WINDOWS\system32\zura\RVHOST.exe
F3 - REG:win.ini: load=C:\DaNeT\RVHOST.exe
F3 - REG:win.ini: load=C:\Jaws\RVHOST.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\babyrina\svchost.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\bugdoll\explorer.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\cipantanah\explorer.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\sexygirl\svchost.exe
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,msi32info.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\(RandomName)\csrss.exe
F3 - REG:win.ini: run=C:\WINDOWS\system32\(RandomName)\csrss.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\(Random Name)\winlogon.exe
F3 - REG:win.ini: run=C:\WINDOWS\system32\(Random Name)\winlogon.exe
O4 - HKLM\..\Run: [Winmplayer] "C:\WINDOWS\system32\KB_(RandomNumber).exe"
F3 - REG:win.ini: load=C:\WINDOWS\system32\camacttiv.exe
F3 - REG:win.ini: run=c:\windows\system\programas\svchost.exe
F3 - REG:win.ini: run=c:\windows\system32\shellext\czvhost.exe
à priori, ça a l'air dû au fait qu'avant le C:\ il n'y a pas d'espace. Le traitement de ces autres lignes, dans lesquelles un espace précède la lettre de lecteur, ne pose aucun pb par exemple :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
O3 - Toolbar: The leosrv - {C31D988D-A314-49BB-BA51-7F57DEE5EA34} - C:\WINDOWS\leosrv.dll
O3 - Toolbar: The leosrv - {C7A4712B-9331-4746-AD61-C675C11B89B9} - C:\WINDOWS\leosrv.dll
O3 - Toolbar: The retnsrp - {CC304A4D-FC79-4CD3-9A67-46E3AF59319D} - C:\WINDOWS\retnsrp.dll
O3 - Toolbar: The leosrv - {DCBF721A-11E3-4FB8-93D6-9AE46178D5B6} - C:\WINDOWS\leosrv.dll
O3 - Toolbar: The leosrv - {F7C394C7-BFBD-4A20-AD14-2AA94424C09C} - C:\WINDOWS\leosrv.dll
O4 - HKLM\..\Run: [ci1gnt] C:\WINDOWS\system32\ci1gnt.exe
O4 - HKCU\..\Run: [ci1gnt] C:\WINDOWS\system32\ci1gnt.exe
O4 - HKLM\..\Run: [clkhost] C:\WINDOWS\dcxxygx.exe
O4 - HKLM\..\Run: [sis32] C:\WINDOWS\system32\winsos.exe
O4 - HKLM\..\Run: [svchost.exe] C:\WINDOWS\svchost.exe
O4 - HKLM\..\Run: [winroot] C:\WINDOWS\system32\winsn.exe
O21 - SSODL: alxvdvm - {Random CLSID} - C:\WINDOWS\alxvdvm.dll
O21 - SSODL: bvtqfvx - {Random CLSID} - C:\WINDOWS\bvtqfvx.dll
Or il me semble que dans la commande que tu suggères :

Code :
sed -n '/ [a-zA-Z]:\\/s/^.* \([a-zA-Z]:\\.*$\)/\1/p' liste.txt
il y a un espace qui est considéré comme le début du délimiteur [a-zA-Z]:\\

J'ai donc un peu modifié en conséquence :

Code :
sed -n '/.[a-zA-Z]:\\/s/^.* \(.*[a-zA-Z]:\\.*$\)/\1/p' 1.txt > 2.txt
qui donne bien les 1479 lignes attendues, sauf que certaines apparaissent de manière insatisfaisante. Une recherche des lignes ne commençant pas par C:\ (donc non attendues) redonne ces 36 lignes :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
egrep -vi "^C" 2.txt
 
UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\explorer.exe
UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ldr.exe
UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ndetect.exe,
UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ntss.exe 
"C:\WINDOWS\system32\ntss.exe" * 
UserInit=C:\WINDOWS\System32\userinit.exe,C:\WINDOWS\System32\wfvs.exe
"C:\WINDOWS\System32\wfvs.exe" *
UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wsnpoem.exe,
UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\codeblocks.exe,
UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wscvs.exe 
"C:\WINDOWS\system32\wscvs.exe" * 
UserInit=C:\WINDOWS\system32\userinit.exe,ntsvc32.dll
"C:\Program Files\svchosts.exe"
UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wspvs.exe
"C:\WINDOWS\system32\wspvs.exe" * 
run=C:\WINDOWS\ServicePackFiles\services.exe
run=C:\WINDOWS\ServicePackFiles\winlogon.exe
"C:\WINDOWS\Help\aolupd.exe"
load=C:\WINDOWS\system32\zura\RVHOST.exe
load=C:\DaNeT\RVHOST.exe
load=C:\Jaws\RVHOST.exe
load=C:\WINDOWS\system32\babyrina\svchost.exe
load=C:\WINDOWS\system32\bugdoll\explorer.exe
load=C:\WINDOWS\system32\cipantanah\explorer.exe
load=C:\WINDOWS\system32\sexygirl\svchost.exe
UserInit=C:\WINDOWS\system32\userinit.exe,msi32info.exe
load=C:\WINDOWS\system32\(RandomName)\csrss.exe
run=C:\WINDOWS\system32\(RandomName)\csrss.exe
load=C:\WINDOWS\system32\(Random Name)\winlogon.exe
run=C:\WINDOWS\system32\(Random Name)\winlogon.exe
"C:\WINDOWS\system32\KB_(RandomNumber).exe"
load=C:\WINDOWS\system32\camacttiv.exe
run=c:\windows\system\programas\svchost.exe
run=c:\windows\system32\shellext\czvhost.exe
UserInit=userinit.exe,C:\WINDOWS\system\lsass.exe
"C:\WINDOWS\SYSTEM32\SUPPRT.RVD" -a
Il faudrait donc ajouter une condition supplémentaire au délimiteur, son début commence :
- soit par un espace puis une lettre de lecteur, un guillemet et enfin un antislash ;
- soit par un caractère spécial (=, ") ou plutot n'importe quel caractère y compris spécial

je n'arrive pas à ajouter cette contrainte dans ta proposition, aurais-tu une piste supplémentaire, stp ?
eZula est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/12/2007, 12h58   #4
Membre expérimenté
 
Avatar de BlaireauOne
 
Inscription : mars 2007
Messages : 469
Détails du profil
Informations personnelles :
Localisation : France, Haute Garonne (Midi Pyrénées)

Informations forums :
Inscription : mars 2007
Messages : 469
Points : 576
Points : 576
La commande suivante doit régler le problème

Code :
1
2
3
sed -n -e '/[ ,="][a-zA-Z]:\\/s/^.*[ ,="]\([a-zA-Z]:\\.*$\)/\1/p' liste.txt | \
sed    -e 's/".*$//' -e 's/,.*$//' > resul.txt
cat resul.txt

Fichier liste.txt :
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 
expression1 pouvant contenir des caractères spéciaux C:\windows\abc.txt
expression2 pouvant contenir des caractères spéciaux C:\windows\system32\z.dll
expression3 pouvant contenir des caractères spéciaux F:\zeighnzcjrt.sys
expression4 pouvant contenir des caractères spéciaux C:\a.inf
O4 - HKLM\..\Run: [Automatic Media Update] "C:\WINDOWS\SYSTEM32\SUPPRT.RVD" -a
F2 - REG:system.ini: UserInit=userinit.exe,C:\WINDOWS\system\lsass.exe
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\explorer.exe
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ldr.exe
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ndetect.exe,
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\ntss.exe
O4 - HKLM\..\Run: [Network Translation System Service] "C:\WINDOWS\system32\ntss.exe" * 
F2 - REG:system.ini: UserInit=C:\WINDOWS\System32\userinit.exe,C:\WINDOWS\System32\wfvs.exe
O4 - HKLM\..\Run: [Windows File Verification Service] "C:\WINDOWS\System32\wfvs.exe" *
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wsnpoem.exe,
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\codeblocks.exe,
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wscvs.exe 
O4 - HKLM\..\Run: [Windows Server Client Verification Service] "C:\WINDOWS\system32\wscvs.exe" * 
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,ntsvc32.dll
O4 - HKLM\..\Run: [Windows Services] "C:\Program Files\svchosts.exe"
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,C:\WINDOWS\system32\wspvs.exe
O4 - HKLM\..\Run: [Windows Server Peer Verification Service] "C:\WINDOWS\system32\wspvs.exe" * 
F3 - REG:win.ini: run=C:\WINDOWS\ServicePackFiles\services.exe
F3 - REG:win.ini: run=C:\WINDOWS\ServicePackFiles\winlogon.exe
O23 - Service: AOL Smart Update Service (AOL_Updater) - Unknown owner - "C:\WINDOWS\Help\aolupd.exe"
F3 - REG:win.ini: load=C:\WINDOWS\system32\zura\RVHOST.exe
F3 - REG:win.ini: load=C:\DaNeT\RVHOST.exe
F3 - REG:win.ini: load=C:\Jaws\RVHOST.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\babyrina\svchost.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\bugdoll\explorer.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\cipantanah\explorer.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\sexygirl\svchost.exe
F2 - REG:system.ini: UserInit=C:\WINDOWS\system32\userinit.exe,msi32info.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\(RandomName)\csrss.exe
F3 - REG:win.ini: run=C:\WINDOWS\system32\(RandomName)\csrss.exe
F3 - REG:win.ini: load=C:\WINDOWS\system32\(Random Name)\winlogon.exe
F3 - REG:win.ini: run=C:\WINDOWS\system32\(Random Name)\winlogon.exe
O4 - HKLM\..\Run: [Winmplayer] "C:\WINDOWS\system32\KB_(RandomNumber).exe"
F3 - REG:win.ini: load=C:\WINDOWS\system32\camacttiv.exe
F3 - REG:win.ini: run=c:\windows\system\programas\svchost.exe
F3 - REG:win.ini: run=c:\windows\system32\shellext\czvhost.exe
etc

Resultat :

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
C:\windows\abc.txt
C:\windows\system32\z.dll
F:\zeighnzcjrt.sys
C:\a.inf
C:\WINDOWS\SYSTEM32\SUPPRT.RVD
C:\WINDOWS\system\lsass.exe
C:\WINDOWS\system32\explorer.exe
C:\WINDOWS\system32\ldr.exe
C:\WINDOWS\system32\ndetect.exe
C:\WINDOWS\system32\ntss.exe
C:\WINDOWS\system32\ntss.exe
C:\WINDOWS\System32\wfvs.exe
C:\WINDOWS\System32\wfvs.exe
C:\WINDOWS\system32\wsnpoem.exe
C:\WINDOWS\system32\codeblocks.exe
C:\WINDOWS\system32\wscvs.exe 
C:\WINDOWS\system32\wscvs.exe
C:\WINDOWS\system32\userinit.exe
C:\Program Files\svchosts.exe
C:\WINDOWS\system32\wspvs.exe
C:\WINDOWS\system32\wspvs.exe
C:\WINDOWS\ServicePackFiles\services.exe
C:\WINDOWS\ServicePackFiles\winlogon.exe
C:\WINDOWS\Help\aolupd.exe
C:\WINDOWS\system32\zura\RVHOST.exe
C:\DaNeT\RVHOST.exe
C:\Jaws\RVHOST.exe
C:\WINDOWS\system32\babyrina\svchost.exe
C:\WINDOWS\system32\bugdoll\explorer.exe
C:\WINDOWS\system32\cipantanah\explorer.exe
C:\WINDOWS\system32\sexygirl\svchost.exe
C:\WINDOWS\system32\userinit.exe
C:\WINDOWS\system32\(RandomName)\csrss.exe
C:\WINDOWS\system32\(RandomName)\csrss.exe
C:\WINDOWS\system32\(Random Name)\winlogon.exe
C:\WINDOWS\system32\(Random Name)\winlogon.exe
C:\WINDOWS\system32\KB_(RandomNumber).exe
C:\WINDOWS\system32\camacttiv.exe
c:\windows\system\programas\svchost.exe
c:\windows\system32\shellext\czvhost.exe
__________________
Loi de Murphy:
La Théorie c'est quand ça ne marche pas mais que l'on sait pourquoi.
La Pratique c'est quand ça marche mais qu'on ne sait pas pourquoi.
Quand la théorie rejoint la pratique ça ne marche pas et on ne sait pas pourquoi.
BlaireauOne est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/12/2007, 13h44   #5
Membre du Club
 
Inscription : mars 2006
Messages : 158
Détails du profil
Informations forums :
Inscription : mars 2006
Messages : 158
Points : 48
Points : 48
Merci, ça marche nickel
eZula est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 02h53.


 
 
 
 
Partenaires

Hébergement Web