C'est parti pour un test :
michel@deb9550:~/Documents/testAwk$ cat >file.txt
Nom Genre Age
---------------------------------------
CAMILLE M 7
CHLOE F 12
CLARA F 11
CLEMENT M 7
EMMA F 6
THEO M 8
michel@deb9550:~/Documents/testAwk$ awk '{ print $1, $2 }' file.txt
Nom Genre
---------------------------------------
CAMILLE M
CHLOE F
CLARA F
CLEMENT M
EMMA F
THEO M
michel@deb9550:~/Documents/testAwk$ awk '/CAMILLE/ { print $1, $3, $2 }' file.txt
CAMILLE 7 M
michel@deb9550:~/Documents/testAwk$ awk '/^C.*[AO]/ { print $1, $3, $2 }' file.txt
CAMILLE 7 M
CHLOE 12 F
CLARA 11 F
michel@deb9550:~/Documents/testAwk$ awk '/^CL/,/^E/ { print $0 }' file.txt
CLARA F 11
CLEMENT M 7
EMMA F 6
michel@deb9550:~/Documents/testAwk$ awk '{ print NR, NF, $0 }' file.txt
1 3 Nom Genre Age
2 1 ---------------------------------------
3 3 CAMILLE M 7
4 3 CHLOE F 12
5 3 CLARA F 11
6 3 CLEMENT M 7
7 3 EMMA F 6
8 3 THEO M 8
michel@deb9550:~/Documents/testAwk$ awk '/CAMILLE/ { OFS="," ; print $2,$1 }' file.txt
M,CAMILLE
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
/^CL/,/^E/ {
print NR, \$0
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
awk: prog.awk:2: print NR, \$0
awk: prog.awk:2: ^ la barre oblique inverse n'est pas le dernier caractère de la ligne
awk: prog.awk:2: print NR, \$0
awk: prog.awk:2: ^ syntax error
michel@deb9550:~/Documents/testAwk$
Zut ! Chez moi, ça bloque ici : C'est juste la barre oblique en question qui est en trop.
Ce qui donnerait, sans la barre :
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
/^CL/,/^E/ {
print NR, $0
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
5 CLARA F 11
6 CLEMENT M 7
7 EMMA F 6
michel@deb9550:~/Documents/testAwk$
Tout le reste est OK :
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
/^CL/,/^E/
BEGIN {
action
}
/filter/,/filter/ { action }
{ action}
END {
action
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
CLARA F 11
CLEMENT M 7
EMMA F 6
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
BEGIN {
FS=" "
OFS=";"
}
{
print $1, $3
}
END {
printf "\nThe file has %d lines\n", NR
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
Nom;Age
---------------------------------------;
CAMILLE;7
CHLOE;12
CLARA;11
CLEMENT;7
EMMA;6
THEO;8
The file has 8 lines
michel@deb9550:~/Documents/testAwk$ awk '/THEO/ { print $1, tolower($1) }' file.txt
THEO theo
michel@deb9550:~/Documents/testAwk$ awk '/CHLOE/ { print $3, int($3/5)}' file.txt
12 2
michel@deb9550:~/Documents/testAwk$ awk 'NR > 2 { printf "%10s %02d %-10s\n", $1,$3, $1}' file.txt
CAMILLE 07 CAMILLE
CHLOE 12 CHLOE
CLARA 11 CLARA
CLEMENT 07 CLEMENT
EMMA 06 EMMA
THEO 08 THEO
michel@deb9550:~/Documents/testAwk$ awk '/CLEM/ { print $1, length($1) }' file.txt
CLEMENT 7
michel@deb9550:~/Documents/testAwk$ awk 'NR >2 { print $1, match($1,"A")}' file.txt
CAMILLE 2
CHLOE 0
CLARA 3
CLEMENT 0
EMMA 4
THEO 0
michel@deb9550:~/Documents/testAwk$ awk 'NR >2 { gsub("A","_",$1) ; print $1 }' file.txt
C_MILLE
CHLOE
CL_R_
CLEMENT
EMM_
THEO
michel@deb9550:~/Documents/testAwk$ awk '{ print $1, substr($1,2,3) }' file.txt
Nom om
--------------------------------------- ---
CAMILLE AMI
CHLOE HLO
CLARA LAR
CLEMENT LEM
EMMA MMA
THEO HEO
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
function gentag(nom,age) {
tmp=tolower(substr(nom,1,3))
return tmp "_" age
}
BEGIN {
FS=" "
OFS=";"
}
{
print $1, $3, gentag($1,$3)
}
END {
print NR , "lines"
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
Nom;Age;nom_Age
---------------------------------------;;---_
CAMILLE;7;cam_7
CHLOE;12;chl_12
CLARA;11;cla_11
CLEMENT;7;cle_7
EMMA;6;emm_6
THEO;8;the_8
8;lines
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
BEGIN {
OFS=","
}
NR <=2 { next }
{
if ( $3 < 11 ) {
ecole="primaire"
} else {
ecole="college"
}
print $1, ecole
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
CAMILLE,primaire
CHLOE,college
CLARA,college
CLEMENT,primaire
EMMA,primaire
THEO,primaire
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
NR <=2 { next }
{
min=1
printf "%-10s", $1
while ( min <= $3 ) {
printf "."
min++
}
printf "\n"
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
CAMILLE .......
CHLOE ............
CLARA ...........
CLEMENT .......
EMMA ......
THEO ........
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
NR <=2 { next }
{
printf "%-10s", $1
for ( min=1 ; min <= $3; min++ ) {
printf "."
}
printf "\n"
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
CAMILLE .......
CHLOE ............
CLARA ...........
CLEMENT .......
EMMA ......
THEO ........
michel@deb9550:~/Documents/testAwk$ cat > prog.awk
{
if ( NR <= 2 ) { next } # skip first 2 lines
tab_age[$2]+=$3
tab_cpt[$2]++
}
END {
for ( genre in tab_age ) {
print genre, " : ", "Moy :", int(tab_age[genre]/tab_cpt[genre]), "ans", "nb :", tab_cpt[genre]
}
}
michel@deb9550:~/Documents/testAwk$ awk -f prog.awk file.txt
F : Moy : 9 ans nb : 3
M : Moy : 7 ans nb : 3
michel@deb9550:~/Documents/testAwk$
Dernière modification par MicP (28-10-2013 10:32:39)