实例1:打印用单引号括起来的字符结果
[root@localhost ~]# awk 'BEGIN{print "\047ok\047"}' t
'ok'实例2:设置输出域分隔符
设置前
[root@localhost ~]# awk -F "." '{print $2,$3}' hosts
100 230214 14215 173设置后(BEGIN可以省略)
[root@localhost ~]# awk -F "." ' BEGIN{OFS="*"} {print $2,$3}' hosts
100*230214*14215*173实例3:找个匹配条件的行,修改某域后,并打印!注意两个print的位置,
第一个{}中的print打印匹配的内容!!!
[root@localhost ~]# awk -F "." 'BEGIN{OFS="."} $1=="221" {$2=122;print}' hosts
221.122.56.162221.122.2.18221.122.196.244[root@localhost ~]# awk -F "." 'BEGIN{OFS="."} $1=="221" {$2=122}{print}' hosts 218.26.171.20218.74.116.104219.235.240.62259.235.240.62221.122.56.162221.122.2.18221.122.196.244实例4:打印字符串出现的次数
[root@localhost ~]# awk '/100/ {cishu++}
END {print cishu}' hosts2[root@localhost ~]# awk '/100/ {cishu++} END {print"100 is found "cishu" times"}' hosts100 is found 2 timessub函数:一条记录中在第一次匹配时执行
gsub函数:在一条记录中所有匹配的域执行
例:
[root@localhost ~]# vim word
baa aaa aaabaa aaa aaaaa aaa aaa[root@localhost ~]# awk '$1~/^aa/ {sub(/aa/,"AA");print}' word
AA aaa aaa[root@localhost ~]# awk '$1~/^aa/ {gsub(/aa/,"AA");print NR,$0}' word --打印行号3 AA AAa AAaindex函数:返回字符串被子字符串第一次匹配的位置(第一个字母的位置)
例1:
[root@localhost ~]# vim word
a1a11ac[root@localhost ~]# awk '{print NR,index($0,"a")}' word
1 12 23 34 0例2:
[root@localhost ~]# awk '{ print index("mytest", "test") }' word
3
3
3
3
因为word必须为存在的文件,可以没有"mytest", "test"字符,word有4行
length返回字符长度
例:
[root@localhost ~]# vim word
219.235.240.62259.235.240.62221.12.56.162221.182.2.18221.208.196.244[root@localhost ~]# awk -F . '{print length}' word
1414131215[root@localhost ~]# awk -F . '{print length($0)}' word 1414131215[root@localhost ~]# awk -F . '{print length($1),length($2),length($3),length($4)}' word 3 3 3 2 --为什么加起来不是14呢,因为没有算3个"."3 3 3 23 2 2 33 3 1 23 3 3 3[root@localhost ~]# awk 'BEGIN{print length("ok")}'2substr函数:截取字符串
例:
[root@localhost ~]# vi word
219.235.240.62259.235.240.62221.12.56.162221.182.2.18221.208.196.244[root@localhost ~]# awk -F "." '{print substr($0, 7,11)}' word
5.240.625.240.62.56.1622.2.188.196.244[root@localhost ~]# awk 'BEGIN{print substr("hello world", 7,11)}'
worldmatch函数:
输出表达式在字符串中匹配的开始位置RSTART,及此位置到被匹配的字符串尾的字符个数RLENGTH
[root@localhost ~]# vim word
219.235.240.62259.235.240.62221.12.56.168221.182.2.18221.208.196.244要求:只打印"以8结尾的$4的最后两个字符串"
[root@localhost ~]# awk -F "." 'match($4,/8$/){print substr($4,RSTART-1,RSTART)}' word
6818[root@localhost ~]# awk -F "." 'match($4,/8$/){print "RSTART IS "RSTART,"RLENGTH IS "RLENGTH,substr($4,RSTART-1,RSTART)}' word
RSTART IS 3 RLENGTH IS 1 68RSTART IS 2 RLENGTH IS 1 18toupper:小写转大写
tolower:大写转小写
[root@localhost ~]# vi w
ABC def[root@localhost ~]# awk '{print tolower($1),toupper($2)}' wabc DEFsplit函数:把给定的字符串重新分割成数据组,如果不FS则FS默认为空格
[root@localhost ~]# date
Wed Jul 4 16:42:45 CST 2012You have new mail in /var/spool/mail/root[root@localhost ~]# awk 'BEGIN{"date" | getline d;split(d,day,":");print day[3]}'47 CST 2012$ awk 'BEGIN{ split( "20:18:00", time, ":" ); print time[2] }'
上例把时间按冒号分割到time数组内,并显示第二个数组元素18
systime函数返回从1970年1月1日开始到当前时间(不计闰年)的整秒数
[root@localhost ~]# awk 'BEGIN{ now = systime(); print now }'
1341394712
strftime函数使用C库中的strftime函数格式化时间
awk '{ now = systime(); print now }'
ORS指定:
[mmcycdl@localhost ~]$ awk 'ORS="\n/gz\n"{print $0}' m2 > m3
[root@localhost ~]# awk 'BEGIN{ now=strftime("%Y%m%d"); print now }'
20120704-------------------------------------------
鸟哥的linux学习笔记:
[root@localhost ~]# cat -A pay.txt Name 1st 2nd 3th$VBird 2300 2400 2500$DMTsai 2100 2000 2300$Bird2 4300 4200 4100$[root@localhost ~]# cat pay.txt | awk 'NR==1{printf("%1s\t %1s\t %1s\t %1s\t %1s\n",$1,$2,$3,$4,"total")} NR>=2{total = $2+$3 +$4; printf("%s\t %d\t %d\t %d\t %d\n",$1,$2,$3,$4,total)}'Name 1st 2nd 3th totalVBird 2300 2400 2500 7200DMTsai 2100 2000 2300 6400Bird2 4300 4200 4100 12600[root@localhost ~]# cat pay.txt | awk '{if (NR==1) printf("%s\t %s\t %s\t %s\t %s\n",$1,$2,$3,$4,"total")} {if (NR>=2) total = $2+$3+$4; printf("%s\t %d\t %d\t %d\t %d\n",$1,$2,$3,$4,total)}'Name 1st 2nd 3th totalName 1 2 3 0VBird 2300 2400 2500 7200DMTsai 2100 2000 2300 6400Bird2 4300 4200 4100 12600
==========2013-06-04关于awk模式中的{}修饰符的使用
[root@localhost ~]# awk --posix '/^1[3458][0-9]{9}$/' mobile.txt
awk --posix '/^1[358][0-9]{9}$/'
或者--re-interval也可以
awk这个地方默认不支持{},除非你加上这两个参数