1 AWK command
pattern {
action }
pattern
Indicates pattern matching and action
indicates operation. Both are optional and are distinguished by curly braces.
{action}
You can append multiple files later, or you can use -f
:
awk 'program' input files
awk -f progfile optional list of input files
Build data:
$ touch emp.data
$ vi emp.data
$ cat emp.data
Beth 4.00 0
Dan 3.75 0
Kathy 4.00 10
Mark 5.00 20
Mary 5.50 22
Susie 4.25 18
2 Extract fields
awk $
extracts fields by 1
starting with the field, 0
then the entire line.
{
print $1, $3 }
{
print $O }
{
print }
3 Calculate the number of fields
NF: number of fields, $NF
indicating the last field.
$ awk ' {print NF, $1, $NF}' emp.data
3 Beth 0
3 Dan 0
...
4 Count the number of lines read
NR:counts the number of lines read so far
$ awk ' {print NR, $0}' emp.data
1 Beth 4.00 0
2 Dan 3.75 0
...
5 Add text to the print field
$ awk '{print "line number:",NR, $0}' emp.data
line number: 1 Beth 4.00 0
line number: 2 Dan 3.75 0
...
6 %
Different formats by printing
$ awk '{printf("total pay for %s is $%.2f\n", $1, $2 * $3)}' emp.data
total pay for Beth is $0.00
total pay for Dan is $0.00
...
# -8表示8个字符并向左对齐
$ awk '{ printf("%-8s $%6.2f\n", $1, $2 * $3) }' emp.data
Beth $ 0.00
Dan $ 0.00
...
7 operators
$ awk '$3>0 {print $1, $2*$3}' emp.data
Kathy 40
Mark 100
...
$ awk '$2 >=5 || $3 >= 20 {print $0}' emp.data
Mark 5.00 20
Mary 5.50 22
$ awk '$1=="Susie" {print $0}' emp.data
Susie 4.25 18
$ awk '!($2<5 && $3<20) {print $0}' emp.data
Mark 5.00 20
Mary 5.50 22
8 BEGIN and END
The special pattern BEGIN
matches before the first line of the first input file is read and END
after the last line of the last file is processed.
# print "", 打印空白行
$ awk 'BEGIN { print "NAME RATE HOURS";print ""}{print}' emp.data
NAME RATE HOURS
Beth 4.00 0
Dan 3.75 0
...
$ awk '{pay=pay+$2*$3}
END {print NR, "employees"
print "total pay is ", pay
print "average pay is", pay/NR}' emp.data
6 employees
total pay is 337.5
average pay is 56.25
9 character processing
# 像处理数字一样处理文本
$ awk '$2>maxrate {maxrate=$2;maxemp=$1}
END {print "highest hourly rate:",maxrate,"for",maxemp}' emp.data
highest hourly rate: 5.50 for Mary
# 字符串连接
$ awk '{names=names $1 " "} END {print names}' emp.data
Beth Dan Kathy Mark Mary Susie
# 打印最后一行
$ awk '{last =$0} END {print last}' emp.data
Susie 4.25 18
10 built-in functions
$ awk '{print $1, length($1)}' emp.data
Beth 4
Dan 3
...
$ awk '{nc=nc+length($0)+1
nw=nw+NF}
END {print NR , "lines,",nw,"words,",nc,"characters"}' emp.data
6 lines, 18 words, 77 characters
11 Control flow
$ awk '$2>6 {n=n+1;pay=pay+$1*$3}
END {if (n>0)
print n, "employees, total pay is",pay
else
print "no employees are paid more than $6/hour"
}' emp.data
no employees are paid more than $6/hour
$ awk '{i=1;while(i <=2){print $1;i=i+1}}' emp.data
$ awk '{for (i=1;i<=3;i++) printf $2}' emp.data
12 Processing Arrays
line[NR]=$0
, assign each row to line
the array.
~$ awk '{line[NR]=$0} END {i=NR;while (i>0){print line[i];i--}}' emp.data
Susie 4.25 18
Mary 5.50 22
...
13 One-liners