awk命令的格式:
awk [options] ‘awk-script‘ input-file ...
options:
-F: 指定Field separator域分隔符
-f: 指定awk-script文件名
awk-script: 指定awk命令
input-file: 指定awk输入文件
awk-script的结构:
由BEGIN,body,END三部分组成
BEGIN格式:
BEGIN { awk-commands }
用于打印表头
初始化变量
BEGIN关键字需要大写
BEGIN关键字可选
BODY格式:
/pattern/ { action }
对于input-file每一行执行一次
END格式:
END { awk-commands }
打印表尾
END关键字需要大写
END关键字可选
print命令:print等同于print $0,$0代表所有记录
# cat employee.txt 101,Johnny Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager # awk -F, ‘{print}‘ employee.txt 101,Johnny Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager # awk -F, ‘{print $0}‘ employee.txt 101,Johnny Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales Manager
print 命令:位置参数$1 $2 ... $n,分别代表一行记录的第1列,第2列...第n列
# awk -F, ‘{print $1,$2}‘ employee.txt 101 Johnny Doe 102 Jason Smith 103 Raj Reddy 104 Anand Ram 105 Jane Miller
awk的内置变量:
FS: Input Field Separator 输入域分隔符
OFS: Output Field Separator 输出域分隔符
RS: Input Record Separator 输入记录分隔符
ORS: Output Record Separator 输出记录分隔符
NR: Number of Records 记录编号
FILENAME: Current File Name 输入文件名
FNR: File " Number of Record " 针对文件的记录编号
NF: Number of Field 字段数
FS:
# awk ‘BEGIN { FS=","; print "UserID\tTitle\n------------" } { print $1,$3 } END { print "------------" }‘ employee.txt UserID Title ------------ 101 CEO 102 IT Manager 103 Sysadmin 104 Developer 105 Sales Manager ------------
OFS:
# awk ‘BEGIN { FS=","; OFS=":";print "UserID\tTitle\n------------" } { print $1,$3 } END { print "------------" }‘ employee.txt UserID Title ------------ 101:CEO 102:IT Manager 103:Sysadmin 104:Developer 105:Sales Manager ------------
RS:
# cat employee-one-line.txt 101,Johnny Doe:102,Jason Smith:103,Raj Reddy:104,Anand Ram:105,Jane Miller # awk ‘BEGIN{RS=":"}{print $1}‘ employee-one-line.txt 101,Johnny 102,Jason 103,Raj 104,Anand 105,Jane # awk ‘BEGIN{FS=",";RS=":"}{print $1}‘ employee-one-line.txt 101 102 103 104 105
ORS:
# cat employee-one-line.txt 101,Johnny Doe:102,Jason Smith:103,Raj Reddy:104,Anand Ram:105,Jane Miller # awk ‘BEGIN{FS=",";RS=":"}{print $1,$2}‘ employee-one-line.txt 101 Johnny Doe 102 Jason Smith 103 Raj Reddy 104 Anand Ram 105 Jane Miller # awk ‘BEGIN{FS=",";RS=":";ORS="\n----\n"}{print $1,$2}‘ employee-one-line.txt 101 Johnny Doe ---- 102 Jason Smith ---- 103 Raj Reddy ---- 104 Anand Ram ---- 105 Jane Miller ----
NR:
# awk ‘BEGIN {FS=",";print "-----------"}{print "Record",NR,"is",$1,$2,$3}‘ employee.txt ----------- Record 1 is 101 Johnny Doe CEO Record 2 is 102 Jason Smith IT Manager Record 3 is 103 Raj Reddy Sysadmin Record 4 is 104 Anand Ram Developer Record 5 is 105 Jane Miller Sales Manager
FILENAME :
# awk ‘BEGIN {FS=",";print "-----------"}{print FILENAME,"Record",NR,"is",$1,$2,$3}‘ employee.txt ----------- employee.txt Record 1 is 101 Johnny Doe CEO employee.txt Record 2 is 102 Jason Smith IT Manager employee.txt Record 3 is 103 Raj Reddy Sysadmin employee.txt Record 4 is 104 Anand Ram Developer employee.txt Record 5 is 105 Jane Miller Sales Manager
FNR:
# awk ‘BEGIN {FS=",";print "-----------"}{print FILENAME,"Record",NR,"is",$1,$2,$3}‘ employee.txt items.txt ----------- employee.txt Record 1 is 101 Johnny Doe CEO employee.txt Record 2 is 102 Jason Smith IT Manager employee.txt Record 3 is 103 Raj Reddy Sysadmin employee.txt Record 4 is 104 Anand Ram Developer employee.txt Record 5 is 105 Jane Miller Sales Manager items.txt Record 6 is 101 HD Camcorder Video items.txt Record 7 is 102 Refrigerator Applicance items.txt Record 8 is 103 MP3 Player Audio items.txt Record 9 is 104 Tennis Racket Sports items.txt Record 10 is 105 Laser Printer Office # awk ‘BEGIN {FS=",";print "-----------"}{print FILENAME,"Record",FNR,"is",$1,$2,$3}‘ employee.txt items.txt ----------- employee.txt Record 1 is 101 Johnny Doe CEO employee.txt Record 2 is 102 Jason Smith IT Manager employee.txt Record 3 is 103 Raj Reddy Sysadmin employee.txt Record 4 is 104 Anand Ram Developer employee.txt Record 5 is 105 Jane Miller Sales Manager items.txt Record 1 is 101 HD Camcorder Video items.txt Record 2 is 102 Refrigerator Applicance items.txt Record 3 is 103 MP3 Player Audio items.txt Record 4 is 104 Tennis Racket Sports items.txt Record 5 is 105 Laser Printer Office
NF:输出结果中,第一列是文件名,第二列是记录编号,第三列是每个记录的字段数
# awk -F, ‘{print FILENAME,NR,NF}‘ employee.txt employee.txt 1 3 employee.txt 2 3 employee.txt 3 3 employee.txt 4 3 employee.txt 5 3
awk变量和操作符:
一元操作符:
+: 返回当前数字
-:取负数
++:自加
--:自减
+:
# cat employee-sal.txt 101,Johnny Doe,CEO,10000 102,Jason Smith,IT Manager,5000 103,Raj Reddy,Sysadmin,4500 104,Anand Ram,Developer,4500 105,Jane Miller,Sales Manager,3000 # awk ‘BEGIN{FS=","}{print +$4}‘ employee-sal.txt 10000 5000 4500 4500 3000
-:
# awk ‘BEGIN{FS=","}{print -$4}‘ employee-sal.txt -10000 -5000 -4500 -4500 -3000
++:pre自加,先自加,再打印变量
# awk ‘BEGIN{FS=","}{print ++$4}‘ employee-sal.txt 10001 5001 4501 4501 3001
--:pre自减,先自减,再打印变量
# awk ‘BEGIN{FS=","}{print --$4}‘ employee-sal.txt 9999 4999 4499 4499 2999
++:post自加,先执行打印变量,再自加
# awk ‘BEGIN{FS=","}{print $4++}‘ employee-sal.txt 10000 5000 4500 4500 3000 # awk ‘BEGIN{FS=","}{$4++;print $4++}‘ employee-sal.txt 10001 5001 4501 4501 3001
--:post自减,先执行打印变量,再自减
# awk ‘BEGIN{FS=","}{print $4--}‘ employee-sal.txt 10000 5000 4500 4500 3000 # awk ‘BEGIN{FS=","}{$4--;print $4--}‘ employee-sal.txt 9999 4999 4499 4499 2999
数学运算:
+: 加
-: 减
*: 乘
/: 除
%: 取模
# cat arit.awk BEGIN{ FS=","; OFS=","; item_discount=0; } { item_discount=$4*20/100; print $1,$2,$3,$4-item_discount,$5-1 } # cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Applicance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 # awk -f arit.awk items.txt 101,HD Camcorder,Video,168,9 102,Refrigerator,Applicance,680,1 103,MP3 Player,Audio,216,14 104,Tennis Racket,Sports,152,19 105,Laser Printer,Office,380,4
字符操作:
# cat string.awk BEGIN { FS=","; OFS=","; string1="Hello"; string2="World"; numberstring="100"; string3=string1" "string2; print "Concatenate string is: " string3; numberstring+=1; print "String to number: " numberstring; } # awk -f string.awk items.txt Concatenate string is: Hello World String to number: 101
赋值运算:
=:赋值
+=: a+=1等同于a=a+1
-=: a-=1等同于a=a-1
*=: a*=2等同于a=a*2
/=: a/=2等同于a=a/2
%=: a%=2等同于a=a%2
# cat total.awk BEGIN { total1=total2=total3=total4=total5=10; total1+= 5; print total1; total2-= 5; print total2; total3*= 5; print total3; total4/= 5; print total4; total5%= 5; print total5; } # awk -f total.awk 15 5 50 2
比较运算:
>:
>=:
<:
<=:
==:
!=:
&&:
||:
# cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Applicance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 # awk -F, ‘$5 <=5‘ items.txt 102,Refrigerator,Applicance,850,2 105,Laser Printer,Office,475,5
# awk -F, ‘$4 < 200‘ items.txt 104,Tennis Racket,Sports,190,20
# awk -F, ‘$4 == 475 && $5 == 5‘ items.txt 105,Laser Printer,Office,475,5
# awk -F, ‘$4 == 475 || $5 == 15‘ items.txt 103,MP3 Player,Audio,270,15 105,Laser Printer,Office,475,5
# awk -F, ‘$3 != "Video"‘ items.txt 102,Refrigerator,Applicance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5
正则表达式操作:
~:匹配
!~:不匹配
# awk -F, ‘$3 ~ "ce"‘ items.txt 102,Refrigerator,Applicance,850,2 105,Laser Printer,Office,475,5 # awk -F, ‘$3 !~ "ce"‘ items.txt 101,HD Camcorder,Video,210,10 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20