Awk

Awk permet d’extraire et manipuler des données organisées par lignes et colonnes dans un fichier texte.
Il contient tous les éléments d’un langage de programmation: conditions, opérations mathématiques, etc.
Le nom “awk” vient du nom de ses inventeurs : Al Aho, Peter Weinberger et Briand Kernighan. Crée en 1978.

Données en entrée

Les données en entrées peuvent provenir

Syntaxe awk

Constantes

awk utilise des variables au cours de son execution:


Instructions awk

Afficher du texte

print

printf

Manipuler du texte

length

index

tolower

toupper

substr

match

sub

split

Manipuler des nombres

Built-in Functions awk


Filtres


Langage de programmation

Commentaires

# Commentaire

If

if(NF<2) {
    print "Un seul champ"
} else if(NF==2) {
   printf "Deux champs"
} else {
   print "Plus de deux champs"
}
print NF<2 ? "Un" : "Plusieurs"

Regex:

Opérateur Opération
~ matche une regex
!~ ne matche pas
open = 0
if ($0~/^---/) {
  open = 1-open
}

For

for(i=1; i<=3; i++) {
    print $i
}
aName["prenom"]="Jean"
aName["nom"]="Dupont"

for(k in aName) {
    print k ": " aName[k]
}
Exemple: Transformer des données tabulaires en un tableau HTML

# Données tabulaire
$ cat <<'NOWDOC' > data
Gretchen Galloway   268 178 256 259 282 139 167
Isaac Steele    253 155 210 195 225 172 202
Wayne Myers 290 283 227 243 128 221 103
Lillith Lee 148 127 260 131 180 125 121
Molly Blackwell 299 143 202 267 222 159 227
Maia Arnold 204 198 294 158 254 205 253
Lev Reese   180 278 156 170 261 283 113
Carlos Guthrie  289 205 117 138 232 278 169
Sophia Buck 112 104 191 112 147 294 280
Vincent Mitchell    270 153 207 175 252 202 233
Buffy Harris    206 107 187 286 286 244 156
Reuben Miles    247 227 170 237 133 188 276
Brendan Fowler  265 166 145 278 170 237 291
Mason Hancock   217 231 271 187 284 179 262
Nigel Boone 236 282 196 143 290 284 129
Gretchen Foreman    276 228 186 223 156 257 161
Serena Goodman  189 145 137 155 211 183 133
Shoshana Velez  281 120 125 199 252 296 287
Eve Hughes  236 176 249 173 158 146 216
NOWDOC

# Script awk pour créer un tableau HTML
$ cat <<'NOWDOC' > script
BEGIN{
    print '<table>';
    print '<th>Name</th>';
    for(i=1; i<=7; i++) {
        print '<th>Round ' i '</th>'
    }
}
{
    print '<tr>';
    for(i=1; i<=8; i++) {
        print '<td>' $i '</td>';
        total[i] += $i
    }
    print '<tr>'
}
END {
    print '<tr>';
    print '<td>Total</td>';
    for(i=2; i<=8; i++) {
        print '<td>' int(total[i]/NR) '</td>'
    }
    print '</tr>';
    print '</table>'
}
NOWDOC

# Appel au script awk sur les données
$ awk -F' ' -f script data | head -20
<table>
<th>Name</th>
<th>Round 1</th>
<th>Round 2</th>
<th>Round 3</th>
<th>Round 4</th>
<th>Round 5</th>
<th>Round 6</th>
<th>Round 7</th>
<tr>
<td>Gretchen</td>
<td>Galloway</td>
<td>268</td>
<td>178</td>
<td>256</td>
<td>259</td>
<td>282</td>
<td>139</td>
<tr>
<tr>

While

i=0
while(i++ < 10) {
     print i
}
i=0
do {
   print i
} while(++i < 10);

Break, continue

break
continue

Variable

Incrémentation

Post-incrémentation (retourne la valeur en cours puis incrémente la variable)

a++
a--

Pré-incrémentation (incrémente la variable puis retourne la nouvelle valeur)

++a
--a

Opérations mathématiques

Opérateur Opération
+ plus
- moins
* multiplication
/ division
% modulo
^ puissance
a  = a + 2
a += 2

Fonctions

function pwr(a, b) {
  return exp(b*log(a))
}
pwr($1, $2)

Multiligne

Par défaut, awk commence par la première ligne du document, exécute les différents blocs, avance d’une ligne, ré-exécute, etc.
Il est également possible de récupérer la valeur de plusieurs lignes dans un bloc: