|
grattage gratuit
loterie gratuite
casino gratuit
quizz gratuit
pari gratuit
jeu de pronostic
tirage au sort
jeux de hasard
tombola gratuite
jeux en flash
jeu en réseau
jeu de stratégie
jeux TV
multi jeux
concours gratuit
|
awk ( un fichier d'aide au
format ps
ou pdf
pour aller plus loin)
La syntaxe et Les options
awk [-Fs] [-v variable] [-f fichier de commandes] 'program'
fichier
-F Spécifie les séparateurs de champs
-v Définie une variable utilisée à
l'intérieur du programme.
-f Les commandes sont lu à partir d'un fichier.
Un fichier admet une stucture du type :
colonne 1 colonne 2 colonne 3 colonne 4 ... ( ligne
1)
colonne 1 colonne 2 colonne 3 colonne 4 ... (ligne
2)
...................
colonne 1 colonne 2 colonne 3 colonne 4 ... (ligne
n)
ou encore
champ1 champ2 champ3 champ 4 ( ligne 1 )
champ1 champ2 champ3 champ 4 (ligne 2)
.....................
champ1 champ2 champ3 champ 4 (ligne 2)
var1 var2 var3 var4 var5 (ligne 1)
var1 var2 var3 var4 var5 (ligne 2)
..................................
var1 var2 var3 var4 var5 (ligne n)
Si on prend un fichier du type fichier.txt nom prénom
age fume
Dupont Nicolas 16 oui
Bruce Pierre 17 non
Sati Paul 21 oui
Farinez Sophie 15 oui
Sater Sandrine 17 oui
Marchal Syndi 17 oui
Awk Compte le nombre de champs d'une ligne courante et place
le résultat dans la variable globale NF comme , il
y a 4 colonnes, NF = 4
awk compte ensuite le ,nombre de ligne du fichier et place
le résultat dans la variable globale NR comme, il
y a 6 lignes, NR = 6
par défaut, la variable globale de séparation
FS est l'espace:ici FS=" " . Elle peut être modifié.
On accède à chaque champs de l'enregistrement
courant par la variable $1,$2,,..,$NF.$N0 correspond à
à toutes les colonnes de l'enregistrement ( fichier).
La variable$NF contient le nombre de champs ( colonnes)de
du fichier courant, la variable $NF correspond donc au dernier
champs non du fichier.
[cr@localhost perl]$ cat fichier.txt
perlfaq4.html*
perlipc.html*
perlop.html*
perlport.html*
perltoot.html*
Exemples :
awk 'END {print NR}' fichier imprime
le nombre total de lignes du fichier
[cr@localhost perl]$ awk 'END {print
NR}' fichier.txt
5
awk '{print $NF}' fichier imprime
le dernier champs de chaque ligne
[cr@localhost perl]$ awk '{print $NF}'
fichier.txt
perlfaq4.html*
perlipc.html*
perlop.html*
perlport.html*
perltoot.html*
[cr@localhost perl]$ ls -l perl*.html
-rwxrwxrwx 1 cr
cr
18958 jan 7 17:35 perlbot.html*
-rwxrwxrwx 1 cr
cr
31524 jan 7 17:35 perldata.html*
-rwxrwxrwx 1 cr
cr
36646 jan 7 17:35 perldsc.html*
-rwxrwxrwx 1 cr
cr
19301 jan 7 17:38 perlfaq1.html*
-rwxrwxrwx 1 cr
cr
34348 jan 7 17:38 perlfaq2.html*
-rwxrwxrwx 1 cr
cr
39843 jan 7 17:38 perlfaq3.html*
-rwxrwxrwx 1 cr
cr
77094 jan 7 17:36 perlfaq4.html*
-rwxrwxrwx 1 cr
cr
59827 jan 7 17:36 perlfaq5.html*
-rwxrwxrwx 1 cr
cr
35370 jan 7 17:36 perlfaq6.html*
-rwxrwxrwx 1 cr
cr
46201 jan 7 17:36 perlfaq7.html*
-rwxrwxrwx 1 cr
cr
63919 jan 7 17:37 perlfaq8.html*
Imprime chaque ligne après
avoir effacé les colonnes contenant 'cr'
[cr@localhost perl]$ ls -l perl*.html|
awk -F "cr" '{ $2 = "" ; print $0 }'
-rwxrwxrwx 1
18958 jan 7 17:35 perlbot.html*
-rwxrwxrwx 1
31524 jan 7 17:35 perldata.html*
-rwxrwxrwx 1
36646 jan 7 17:35 perldsc.html*
-rwxrwxrwx 1
19301 jan 7 17:38 perlfaq1.html*
-rwxrwxrwx 1
34348 jan 7 17:38 perlfaq2.html*
-rwxrwxrwx 1
39843 jan 7 17:38 perlfaq3.html*
-rwxrwxrwx 1
77094 jan 7 17:36 perlfaq4.html*
-rwxrwxrwx 1
59827 jan 7 17:36 perlfaq5.html*
-rwxrwxrwx 1
35370 jan 7 17:36 perlfaq6.html*
-rwxrwxrwx 1
46201 jan 7 17:36 perlfaq7.html*
-rwxrwxrwx 1
63919 jan 7 17:37 perlfaq8.html*
Affichons les fichiers perl*.html
répertoire avec la commande ls
-l perl*.html
[cr@localhost perl]$ ls -l perl*.html
-rwxrwxrwx 1 cr
cr
18958 jan 7 17:35 perlbot.html*
-rwxrwxrwx 1 cr
cr
31524 jan 7 17:35 perldata.html*
-rwxrwxrwx 1 cr
cr
36646 jan 7 17:35 perldsc.html*
-rwxrwxrwx 1 cr
cr
19301 jan 7 17:38 perlfaq1.html*
-rwxrwxrwx 1 cr
cr
34348 jan 7 17:38 perlfaq2.html*
-rwxrwxrwx 1 cr
cr
39843 jan 7 17:38 perlfaq3.html*
-rwxrwxrwx 1 cr
cr
77094 jan 7 17:36 perlfaq4.html*
-rwxrwxrwx 1 cr
cr
59827 jan 7 17:36 perlfaq5.html*
-rwxrwxrwx 1 cr
cr
35370 jan 7 17:36 perlfaq6.html*
-rwxrwxrwx 1 cr
cr
46201 jan 7 17:36 perlfaq7.html*
-rwxrwxrwx 1 cr
cr
63919 jan 7 17:37 perlfaq8.html*
-rw-rw-r-- 1 cr
cr
35042 jan 7 17:52 perlfaq9.html
-rwxrwxrwx 1 cr
cr
20078 jan 7 17:35 perl.html*
-rwxrwxrwx 1 cr
cr
71363 jan 7 17:35 perlipc.html*
-rwxrwxrwx 1 cr
cr
12192 jan 7 17:35 perllol.html*
-rwxrwxrwx 1 cr
cr
28067 jan 7 17:34 perlobj.html*
-rwxrwxrwx 1 cr
cr
92747 jan 7 17:34 perlop.html*
-rw-rw-r-- 1 cr
cr
13656 jan 7 17:47 perl_path.html
-rwxrwxrwx 1 cr
cr
12963 jan 7 17:34 perlpod.html*
-rwxrwxrwx 1 cr
cr
71942 jan 7 17:34 perlport.html*
-rwxrwxrwx 1 cr
cr
34032 jan 7 17:33 perlref.html*
-rwxrwxrwx 1 cr
cr
50630 jan 7 17:34 perlre.html*
-rwxrwxrwx 1 cr
cr
10241 jan 7 17:33 perlstyle.html*
-rwxrwxrwx 1 cr
cr
58253 jan 7 17:33 perlsub.html*
-rwxrwxrwx 1 cr
cr
30358 jan 7 17:33 perlsyn.html*
-rwxrwxrwx 1 cr
cr
36746 jan 7 17:33 perltie.html*
-rwxrwxrwx 1 cr
cr
84302 jan 7 17:32 perltoot.html*
Il y a ici 9 colonnes
On peut afficher les fichiers, dont
la taille est supérieure à 70000 et on affiche
seulement le nom colonne 9
[cr@localhost perl]$ ls -l perl*.html|
awk '$5 > 70000 { print $9 }'
perlfaq4.html*
perlipc.html*
perlop.html*
perlport.html*
perltoot.html*
[cr@localhost perl]$
On peut afficher les fichiers, dont
la taille est supérieure à 70000 et on affiche
seulement les colonnes 5 et 9
[cr@localhost perl]$ ls -l perl*.html|
awk '$5 > 70000 { print $5 " " $9 }'
77094 perlfaq4.html*
71363 perlipc.html*
92747 perlop.html*
71942 perlport.html*
84302 perltoot.html*
[cr@localhost perl]$
Exemple :
La commande tee permet de doubler un fichier pour le transmettre
à la fois en sortie et dans le fichier donné
en paramètre. La commande tee, transmet vers la sortie
standard et dans le fichier fichier.txt ( on stocke le résultat).
[cr@localhost perl]$ ls -l perl*.html|
awk '$5 > 70000 { print $9 }' |tee
fichier.txt
perlfaq4.html*
perlipc.html*
perlop.html*
perlport.html*
[cr@localhost perl]$ ls -l fichier.txt
-rw-r--r-- 1 cr cr 72 f 9 15:09 fichier.txt
[cr@localhost perl]$ cat
fichier.txt
perlfaq4.html*
perlipc.html*
perlop.html*
perlport.html*
|
|