转:http://blog.163.com/njut_wangjian/blog/static/165796425201611100261205/
awk基本语法:http://www.runoob.com/linux/linux-comm-awk.html
awk文章系列,将会详细系统地介绍awk常用的用法。首先介绍最常用的字符串函数。
[root@MySQL ~]# awk 'BEGIN{a[0]="a";a[1]="c";a[2]="b";print "before sorting:";for(i in a){print i,a[i]};asort(a);print "after sorting:";for(i in a){print i,a[i]}}'
before sorting:
0 a
1 c
2 b
after sorting:
1 a
2 b
3 c
[root@MySQL ~]# awk 'BEGIN{a[0]="a";a[1]="c";a[2]="b";print "before sorting:";for(i in a){print i,a[i]};asort(a,d);print "after sorting:";for(i in a){print i,a[i]};print ;for(i in d){print i,d[i]}}'
before sorting:
0 a
1 c
2 b
after sorting:
0 a
1 c
2 b
1 a
2 b
3 c
注:sub和gsub函数功能相同,前者指替换匹配的第一个字符串,而后者进行全局替换[root@MySQL ~]# awk 'BEGIN{r="or|ll";s="wj";t="hello,world!hello,awk";print sub(r,s,t),t}'
1 hewjo,world!hello,awk
[root@MySQL ~]# awk 'BEGIN{r="or|ll";s="wj";t="hello,world!hello,awk";print gsub(r,s,t),t}'
3 hewjo,wwjld!hewjo,awk
[root@MySQL ~]# echo "hello,world;hello,awk"|awk '{r="or|ll";s="wj";print sub(r,s),$0}'
1 hewjo,world;hello,awk
[root@MySQL ~]# echo "hello,world;hello,awk"|awk '{r="or|ll";s="wj";print gsub(r,s),$0}'
3 hewjo,wwjld;hewjo,awk注:正则表达式另外一种写法
[root@MySQL ~]# echo "hello,world;hello,awk"|awk '{s="wj";print gsub(/or|ll/,s),$0}'
3 hewjo,wwjld;hewjo,awk
#省略参数t
[root@MySQL ~]# echo "hello,world\!hello,awk\!hello linux\!"|awk 'BEGIN{s="ww";r="ll"}{print gensub(r,s,"g")}'
hewwo,world\!hewwo,awk\!hewwo linux\!
#参数h以g或G开头
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"g",t)}'
hewwo,world!hewwo,awk!hewwo linux!
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"g1",t)}'
hewwo,world!hewwo,awk!hewwo linux!
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"G1",t)}'
hewwo,world!hewwo,awk!hewwo linux!
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"g1",t)}'
hewwo,world!hewwo,awk!hewwo linux!
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"G1",t)}'
hewwo,world!hewwo,awk!hewwo linux!#如果参数h不是数字也不以g或G开头,则替换第一处
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"a",t)}'
hewwo,world!hello,awk!hello linux!
#参数h是数字
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"1",t)}'
hewwo,world!hello,awk!hello linux!
[root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,"3",t)}'
hello,world!hello,awk!hewwo linux![root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,3,t)}'
hello,world!hello,awk!hewwo linux![root@MySQL ~]# awk 'BEGIN{s="ww";t="hello,world!hello,awk!hello linux!";r="ll";print gensub(r,s,0,t)}'
awk: warning: gensub: third argument of 0 treated as 1
hewwo,world!hello,awk!hello linux!
注意:当字符串t为空时,返回的索引为1[root@MySQL ~]# awk 'BEGIN{s="hello,world";t="llo";print index(s,t)}'
3
[root@MySQL ~]# awk 'BEGIN{s="hello,world";t="lloo";print index(s,t)}'
0
[root@MySQL ~]# awk 'BEGIN{s="hello,world";t="hello,world!";print index(s,t)}'
0
[root@MySQL ~]# awk 'BEGIN{s="hello,world";t="";print index(s,t)}'
1
[root@MySQL ~]# awk 'BEGIN{s="hello,world";t="";print index(s,t)}'
1
[root@MySQL ~]# awk 'BEGIN{s="";print length(s)}'
0
[root@MySQL ~]# awk 'BEGIN{s=123;print length(s)}'
3
[root@MySQL ~]# awk 'BEGIN{s="hello world";print length(s)}'
11
[root@MySQL ~]# awk 'BEGIN{print length()}'
0
[root@MySQL ~]# echo "123 345" | awk '{print length()}'
7
案例二:提供参数数组a(正则表达式中没有带括号的子表达式)[root@MySQL ~]# awk 'BEGIN{s="hello,world!";r="ll";print match(s,r)}' #匹配ll,索引为3,返回值为3
3
[root@MySQL ~]# awk 'BEGIN{s="hello,world!";r="wj";print match(s,r)}' #匹配wj,没有匹配上,返回值为0
0
案例三:提供参数数组a(正则表达式中有带括号的子表达式)[root@MySQL ~]# awk 'BEGIN{s="hello,world!";r="ll";print match(s,r,a);print ;for(i in a){print "subscript:"i"\t""valus:"a[i]}}'
3 #返回s中匹配ll的索引,为3
subscript:0 start valus:3 #数组a[0,"start"],值为s中匹配ll的索引,即3
subscript:0 length valus:2 #数组a[0,"length"],值为匹配的字符串的长度,即ll的长度,为2
subscript:0 valus:ll #数组a[0],值为匹配的字符串,即ll另外一种写法,将正则表达式放在//中,和上面是同样的效果
[root@MySQL ~]# awk 'BEGIN{s="hello,world!";print match(s,/ll/,a);print ;for(i in a){print "subscript:"i"\t""valus:"a[i]}}'
3
subscript:0start valus:3
subscript:0length valus:2subscript:0 valus:ll
[root@MySQL ~]# awk 'BEGIN{s="hello,world!";r="(ll).*(or.*d)";print match(s,r,a);print length(a);print ;for(i in a){print "subscript:"i"\t""valus:"a[i]}}'
3 #匹配的字符串索引位置
9 #数组a中的元素个数
subscript:0start valus:3
subscript:0length valus:9
subscript:1start valus:3
subscript:2start valus:8
subscript:0 valus:llo,world
subscript:1 valus:ll
subscript:2 valus:orld
subscript:2length valus:4
subscript:1length valus:2
#上面输出数组a的元素顺序有点乱,整理下,如下:
subscript:0 valus:llo,world
subscript:0 start valus:3
subscript:0 length valus:9
subscript:1 valus:ll
subscript:1 start valus:3
subscript:1 length valus:2
subscript:2 valus:orld
subscript:2 start valus:8
subscript:2 length valus:4当正则表达式中有带括号的子表达式时,数组a中的第0个元素为正则表达式的完整表达式,数组第1-n个元素为正则表达式中子表达式的内容
(ll).*(or.*d)
对于字符串“hello,world!”来说,
正则表达式(ll).*(or.*d)的的完整匹配为“llo,world”,所以a[0]的值为“llo,world”,a[0,"start"]为“llo,world”中的起始字符“l”在“hello,world!”中的索引,即3;a[0,"length"]为“llo,world”的长度,即9。
正则表达式(ll).*(or.*d)中的子表达式分别为(ll)和(or.*d),匹配“hello,world!”时,分别匹配"ll"和“orld”,所以a[1]和a[2]的值分别为"ll"和“orld”,a[n, "start"]和a[n, "length"](n=2,3)分别存储对应的索引和长度
RSTART The index of the first character matched by match (); 0 if no match . ( This implies that character indices start at one .)RLENGTH The length of the string matched by match (); - 1 if no match .
[root@MySQL ~]# awk 'BEGIN{s="hello,world;hello,awk";r=",";print split(s,a,r);for(i in a){print i,a[i]}}'
3
1 hello
2 world;hello
3 awk[root@MySQL ~]# awk 'BEGIN{s="hello,world;hello,awk";r="hello";print split(s,a,r);for(i in a){print i,a[i]}}'
3
1
2 ,world;
3 ,awk
[root@MySQL ~]# head -n 1 /etc/passwd|awk 'BEGIN{s="hello,world;hello"}{print split(s,a);for(i in a){print i,a[i]}}'
1
1 hello,world;hello[root@MySQL ~]# head -n 1 /etc/passwd|awk 'BEGIN{FS=";";s="hello,world;hello"}{print split(s,a);for(i in a){print i,a[i]}}'
2
1 hello,world
2 hello注:FS变量的赋值也可以放在pattern+action外面
[root@MySQL ~]# head -n 1 /etc/passwd|awk -v FS=";" 'BEGIN{s="hello,world;hello"}{print split(s,a);for(i in a){print i,a[i]}}'
2
1 hello,world
2 hello
[root@MySQL ~]# awk 'BEGIN{s="123";print strtonum(s)}'
123
[root@MySQL ~]# awk 'BEGIN{s="0123";print strtonum(s)}'
83
[root@MySQL ~]# awk 'BEGIN{s="0x123";print strtonum(s)}'
291
[root@MySQL ~]# awk 'BEGIN{s="0X123";print strtonum(s)}'
291
[root@MySQL ~]# awk 'BEGIN{s="a123";print strtonum(s)}'
0
[root@MySQL ~]# awk 'BEGIN{s="12a3";print strtonum(s)}'
12
[root@MySQL ~]# awk 'BEGIN{s="123a";print strtonum(s)}'
123
[root@MySQL ~]# awk 'BEGIN{s="123.456";print strtonum(s)}'
123.456
[root@MySQL ~]# awk 'BEGIN{s="";print strtonum(s)}'
0
[root@MySQL ~]# awk 'BEGIN{s="hello,world";print substr(s,2)}'
ello,world
[root@MySQL ~]# awk 'BEGIN{s="hello,world";print substr(s,2,5)}'
ello,
[root@MySQL ~]# awk 'BEGIN{s="hello,world";print substr(s,0)}'
hello,world
[root@MySQL ~]# awk 'BEGIN{s="hello,world";print substr(s,-2)}'
hello,world
[root@MySQL ~]# awk 'BEGIN{s="hello,world";print substr(s,3,-2)}'
[root@MySQL ~]#
[root@MySQL ~]# awk 'BEGIN{s="HellO";print tolower(s)}'
hello
[root@MySQL ~]# awk 'BEGIN{s="^He;llO$";print tolower(s)}'
^he;llo$
[root@MySQL ~]# awk 'BEGIN{s="HellO";print toupper(s)}'
HELLO
[root@MySQL ~]# awk 'BEGIN{s="^He;llO$";print toupper(s)}'
^HE;LLO$