An awk program is composed of a cycle of the main input. A loop is a routine and will be executed repeatedly until some conditions exist to terminate it. The main input loop is a routine. Number of cycles = number of objects.
Awk can write two special routines, begin and end.
Cat name
=>
John Robinson, Koren inc., 978 4th Ave., Boston, MA: 01760,696-0987
Phyllis Chapman, Gve Corp., 34 sea drive, amesbury, MA: 01881,879-0900
Bulent green, wR Corp., 46 win, Alameda, MA: 93253,356-3635
Lily Smith, beyondsoft Corp., 28 win, Beijing, BJ: 2535,010-4546
Specify a separator in the awk
Use the-F parameter
Awk-F "/t" '{print $1}' # use a tab as the Separator
Use FS system variables
Awk 'in in {FS = ","} {print $1} 'name # fs is a space by default.
You can also use regular expressions to specify separators.
Awk-F "[,:]" '{print $6}' name
Determine whether the fields match in the awk
Sed-n'/^ $ /! P' name | awk-F "," '$5 !~ /MA/{print $1 "," $6 }'
=>
Lily Smith, 010-4546
Use of variables in awk
Awk '/^ $/{x ++} end {print x}' name # Number of printed Spaces
=> 3
Cat scre
=>
John 85 92 78 94 88
Andrea 89 90 75 90 86
Jasper 84 88 80 92 84
Awk '{Total = $2 + $3 + $4 + $5 + $6; AVG = total/5; print $1, AVG}' score # print the name and average score
=>
John 87.4
Andrea 86
Jason 85.6