Lodsq sqhhaa
Soit le fichier tel suivant : $ cat tel Luc Louis Annie Josee Dupuis Gauthier Julien Cote 123-1111 146-1111 321-1234 561-23400 TR Qc Mtl MTL M M F F
$ nawk '{print $0}' tel $ nawk '{print $1}' tel $ nawk '{print $4}' tel $ nawk '{print $6}' tel
# la ligne au complet # le prénom # la ville # pas d'erreur mais des lignes vides 1 : Luc 2 : Louis 3 : Annie 4 : Josee # seulement les hommes #équivalent
$ nawk '{print NR, ":",$1}' tel $ nawk 'NR>1 { print NR, ":",$1 }' tel $ nawk '{$3="***"; print $0}' tel #modification
$ nawk '{ if ($5 == "M") print NR, ":",$1 }' tel $ nawk '$5 == "M" { print NR, ":",$1 }' tel
# affichage formatée : no,prénom,ville (p.57 volume AWK) : PRINTF $ nawk '$5 == "M" { printf ("%i : %s \t %s\n", NR, $1,$4) }' tel # préformage du début : BEGIN $ nawk 'BEGIN { printf("No Prénom\tVille\n\n") } $5 == "M" { printf ("%i : %s \t %s\n", NR, $1,$4) }' tel On peut aussi utiliser un programme shell : ex1 (Ce fichier est exécutable) #!/bin/sh # exemple NAWK echo "Début de AWK" echo nawk ' BEGIN { printf("No Prénom\tVille\n** ******
********\n") }
$5 == "M" { printf ("%i : %s \t %s\n", NR, $1,$4); total++ } END {print "Il y a", total " homme(s)."} ' tel echo echo "Fin de AWK" L'appel avec le fichier se ferait comme suit : ex1
On peut utiliser un fichier de code AWK : ex1awk (Ce fichier n'est pas exécutable) BEGIN { printf("No Prénom\tVille\n** ****** total=0; } $5 == "M" { printf ("%i : %s \t %s\n", NR, $1,$4); total++ } END { print "Il y a", total " homme(s)." } L'appel avec le fichier se ferait comme suit : nawk -f ex1awk tel # Séparateur différent d'espace ou de TAB : option –F Soit le fichier notes ayant le contenu suivant : BOB:18:8:15 MARIE:6:11:0 PIERRE:15:0:20 LUCIE:16:16:17 LOUISE:10:20:15 PAUL:16:18:12 $ nawk –F: ' {print $0}' notes $ nawk –F: ' {print $1}' notes $ nawk –F: ' {print $4}' notes # Moyenne des notes par étudiants $ nawk –F: '{print $1 "\t moyenne: " ($2+$3+$4)/3} ' notes ou encore