Awk

De Linuxmemo.

(Différences entre les versions)
Ligne 12 : Ligne 12 :
  Alice
  Alice
  Orville
  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

Version du 12 avril 2018 à 14:17

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
Outils personnels