Awk

De Linuxmemo.

awk 'instructions' files
awk -f script files

Awk, in the usual case, interprets each input line as a record and each word on that line, delimited byspaces or tabs, as a field. (These defaults can be changed.) One or more consecutive spaces or tabs count as a single delimiter. Awk allows you to reference these fields, in either patterns or procedures. $0 represents the entire input line. $1, $2, ... refer to the individual fields on the input line.

awk '{ print $1 }' list
John
Alice
Orville

Print the first word of each line containing the string "MA". We can say "word" because by default awk separates the input into fields using either spaces or tabs as the field separator " ".

awk '/MA/ { print $1 }' list
John
Eric
Sal

we use the -F option to change the field separator to a comma.

awk -F, '/MA/ { print $1 }' list
John Daggett
Eric Adams
Sal Carpenter

Multiple commands are separated by semicolons.

awk -F, '{ print $1; print $2; print $3 }' list

Variables

Note that we don't have to assign to a variable before using it (because awk variables are initialized to the empty string).

Outils personnels