$ nawk '{print $0}' tel $ nawk '{print $0}' tel $ nawk '{print $1}' tel $ nawk '{print $4}' tel $ nawk '{print $6}' tel $ nawk '{print NR, ":",$1}' tel $ nawk 'NR>1 { print NR, ":",$1 }' tel $ nawk '{$3="***"; print $0}' tel $ nawk '{ if ($5 == "M") print NR, ":",$1 }' tel $ nawk '$5 == "M" { print NR, ":",$1 }' tel $ nawk '$5 == "M" { printf ("%i : %s \t %s\n", NR, $1,$4) }' tel $ nawk 'BEGIN { printf("No Prnom\tVille\n\n") } $5 == "M" { printf ("%i : %s \t %s\n", NR,$1,$4) }' tel On peut directement crire un programme shell contenant toutes ces commandes : #!/bin/sh # exemple NAWK echo "Dbut de AWK" Page 3 Ou crire un fichier exoawk (attention il nest pas executable): echo "Dbut de AWK" echo nawk ' BEGIN { printf("No Prnom\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" Le fichier exoawk BEGIN { printf("No Prnom\tVille\n** ****** ********\n"); total=0; Page 4 Lappel se fait par : $nawk -f exoawk tel total=0; } $5 == "M" { printf ("%i : %s \t %s\n", NR, $1,$4); total++ } END { print "Il y a", total " homme(s)." } Soit le fichier notes ayant le contenu suivant : Quaffiche la commande : Page 5 $ nawk F: ' {print $0}' notes $ nawk F: ' {print $1}' notes $ nawk F: '{print $1 "\t moyenne: " ($2+$3+$4)/3} ' notes $ nawk F: 'BEGIN {printf("Nom\tMoyenne\n")}{printf("%s\t%5.2f\n",$1,($2+$3+$4)/3)} ' notes Donner laffichage du programme shell suivant : Page 6 Page 7 reformatage du fichier tel avec les : comme sparateur $cat tel | tr -s " " ":" > tel2 nawk -F: ' BEGIN { printf ("Verification du format des telephones\n") } $3 ~ /^[0-9][0-9][0-9][-][0-9][0-9][0-9][0-9]$/ { print $1, " tel: ", $3, " Format valide " } $3 !~ /^[0-9][0-9][0-9][-][0-9][0-9][0-9][0-9]$/ { print $1, " tel: ", $3, " FORMAT INVALIDE" } END { printf ("\nFin de vrification\n\n") } ' tel2 Page 8 1. Faites afficher la table de multiplication du chiffre 5 2. Faites afficher la table de multiplication du chiffre pass en paramtre 3. Faites la moyenne des lves et du groupe du fichier notes. 4. Imprimez le nombre total d'utilisateur du systme 5. Imprimez le nom des fichiers du rpertoire courant 6. Numrotez les lignes d'un fichier pass en paramtre 7. Comptez le nombre de lignes et de mots d'un fichier texte 8. Avec le contenu du fichier pstxt Page 9 #!/bin/sh # multiplication table de 5 : awk1 nawk BEGIN { printf("Table de multiplication du 5\n") for (r=1; r<=12;r++) { produit=r*5 printf("%2d fois 5 = %5d\n",r,produit) } }' Page 10 } }' echo Fin du programme #!/bin/sh # multiplication table d'un nombre : awk2 nawk v nb=$1 ' BEGIN { printf("Table de multiplication de %2d\n",nb) for (r=1; r<=12;r++) { produit=r*nb printf("%2d fois %2d = %5d\n",r,nb,produit) } }' echo Fin du programme #!/bin/sh # moyenne : awk3 nawk F: BEGIN { printf("Moyenne des lves\n\n") total=0; nb=0; note=0 } { note=($2+$3+$4); printf(" %s \t: %5.2f \n", $1, note/3) nb++; total=total+(note/3) } Page 11 } END { printf("\nMoyenne du groupe : %6.2f\n",total/nb) } ' notes echo Fin du programme Awk4 $who -a | nawk ' {print $0} END {print NR-1, " utilisateurs branch"}' Awk5 $ls l | tail +2 | nawk v rep=`pwd` ' BEGIN { print "Fichier du rpertoire courant : ", rep } {print $NF; nb++} END {print nb, " fichiers"}' #!/bin/sh # compte les lignes dans un fichier #awk6 Page 12 #awk6 if [ $# = 0 ] then echo "Il faut un nom de fichier comme paramtre" exit 1 fi nawk v fich=$1 ' { print $0;nb++} END {print nb, "lignes dans le fichier ", fich} ' $1 echo echo Fin du programme