$ # awk can be used for checking that data has reasonable values and is in the right format$ # ex: check number of fields$ # if there are no error, there's no output$ echo 'Beth 4.00 0Dan 3.75 0Kathy 4.00 10Mark 5.00 20 another_fieldMary 5.50 22Susie 4.25 18' | awk 'NF != 3 { print $0, "number of fields is not equal to 3" }'Mark 5.00 20 another_field number of fields is not equal to 3$ echo 'Beth 4.00 0Dan 3.75 0Kathy 4.00 10Mark 5.00 20Mary 5.50 22Susie 4.25 18' | awk '$2 < 4.00 { print $0, "rate is below minimum wage" }'Dan 3.75 0 rate is below minimum wage
$ # if/else$ echo 'Beth 4.00 0Dan 3.75 0Kathy 4.00 10Mark 5.00 20Mary 5.50 22Susie 4.25 18' | awk '$2 > 4 { n = n + 1; pay = pay + $2 * $3 }END { if (n > 0) printf("%d employees, total pay is %d, average pay is %.2f\n", n, pay, pay/n) else print "no employees are paid more than $6/hour" }'3 employees, total pay is 297, average pay is 99.17$ # while: condition + body$ echo 'Beth 4.00 1Dan 3.75 2Kathy 4.00 10Mark 5.00 20Mary 5.50 22Susie 4.25 18' | awk '{ i = 1 while (i <= 3) { printf("%d\t%.2f\n", i, $2 * (1 + $3) ^ i) i = i + 1 }}'1 8.002 16.003 32.001 11.252 33.753 101.251 44.002 484.003 5324.001 105.002 2205.003 46305.001 126.502 2909.503 66918.501 80.752 1534.253 29150.75$ # for$ echo 'Beth 4.00 1Dan 3.75 2Kathy 4.00 10Mark 5.00 20Mary 5.50 22Susie 4.25 18' | awk '{ for (i = 1; i <= 3; i = i + 1) printf("%d\t%.2f\n", i, $2 * (1 + $3) ^ i)}'1 8.002 16.003 32.001 11.252 33.753 101.251 44.002 484.003 5324.001 105.002 2205.003 46305.001 126.502 2909.503 66918.501 80.752 1534.253 29150.75
Arrays
$ # arrays for storing groups of related values$ # first remember each input line$ # then print lines in reverse order in the END action$ echo 'Beth 4.00 1Dan 3.75 2Kathy 4.00 10Mark 5.00 20Mary 5.50 22Susie 4.25 18' | awk ' { line[NR] = $0 }END { i = NR while (i > 0) { print line[i] i = i - 1 } }'Susie 4.25 18Mary 5.50 22Mark 5.00 20Kathy 4.00 10Dan 3.75 2Beth 4.00 1
Tips
$ # print the total number of input lines$ awk 'END { print NR }'$ # print the tenth input line$ awk 'NR == 10'$ # print the last field of every input line$ awk '{ print $NF }'$ # print the last field of the last input line$ awk ' { field = $NF }END { print field }'$ # print every input line with more than four fields$ awk 'NF > 4'$ # print every input line in which the last field is more than 4$ awk '$NF > 4'$ # print the total number of fields in all input lines$ awk ' { nf = nf + NF}END { print nf }'$ # print the total number of lines that contains `Betch`$ awk '/Beth/ { nlines = nlines + 1 }END { print nlines }'$ # print the largest first field and the line that contains it$ awk '$1 > max { max = $1; maxline = $0 }END { print max, maxline }'$ # print every line longer than 80 characters$ awk 'length($0) > 80'$ # print the number of fields in every line followed by the field itself$ awk '{ print NF, $0 }'$ # exchange the first two fields of every line$ awk '{ temp = $1; $1 = $2; $2 = temp; print }'$ # print every line after erasing the second field$ awk '{ $2 = ""; print }'$ # print every line with line number$ awk '{ print NR, $0 }'$ # print the sums of the fields of every line$ awk '{ sum = 0 for (i = 1; i <= NF; i = i + 1) sum = sum + $i print sum}'