实例1:将每件单独的商品价格减少 20% 并且将每件单独的商品的数量减少 1
[root@localhost ~]# cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 [root@localhost ~]# cat dis.awk BEGIN{ FS=","; OFS=","; discount=0 } { discount=$4*20/100; print $1,$2,$3,$4-discount,$5-1 } [root@localhost ~]# awk -f dis.awk items.txt 101,HD Camcorder,Video,168,9 102,Refrigerator,Appliance,680,1 103,MP3 Player,Audio,216,14 104,Tennis Racket,Sports,152,19 105,Laser Printer,Office,380,4实例2:只打印偶数行
[root@localhost ~]# awk 'NR%2==0' items.txt 102,Refrigerator,Appliance,850,2 104,Tennis Racket,Sports,190,20 四、字符串操作符(空格)是连接字符串的操作符。
实例1:
[root@localhost ~]# cat str.awk BEGIN{ FS=","; OFS=","; str1="Audio"; str2="Video"; nustr="100"; str3=str1 str2; print "Concatenate string is:"str3; nustr=nustr+1; print "Str to nu:"nustr; } [root@localhost ~]# awk -f str.awk items.txt Concatenate string is:AudioVideo Str to nu:101 四、赋值操作符实例1:
[root@localhost ~]# cat fz.awk BEGIN { FS=","; OFS=","; 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; } [root@localhost ~]# awk -f fz.awk items.txt 15 5 50 2 0实例2:打印商品清单
[root@localhost ~]# cat items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5 [root@localhost ~]# awk -F, ' >BEGIN{ total=0} { total +=$5 } >END { print "Total Qutantity:" total }' items.txt Total Qutantity:52 五、比较操作符实例1:打印数量小于等于临界值 5 的商品信息
[root@localhost ~]# awk -F, '$5<=5' items.txt 102,Refrigerator,Appliance,850,2 105,Laser Printer,Office,475,5实例2:打印编号为 103 的商品信息
[root@localhost ~]# awk -F, '$1==103' items.txt 103,MP3 Player,Audio,270,15实例3:打印除 Video 以外的所有商品
[root@localhost ~]# awk -F, '$3!="Video"' items.txt 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5实例4:同实例3,但只打印描述信息
[root@localhost ~]# awk -F, '$3!="Video"{print $2}' items.txt Refrigerator MP3 Player Tennis Racket Laser Printer实例5:打印价钱低于 900 或者数量小于等于临界值 5 的商品信息
[root@localhost ~]# awk -F, '$5<=5||$4<900' items.txt 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 104,Tennis Racket,Sports,190,20 105,Laser Printer,Office,475,5实例6:打印/etc/password 中最大的 UID(以及其所在的整行)。
[root@localhost ~]# awk -F ':' ' >$3 > maxuid >{ maxuid = $3; maxline = $0 } >END { print maxuid,maxline }' /etc/passwd 1009 user3:x:1009:1010::/home/user3:/bin/bash实例8:打印/etc/passwd 中 UID 和 GROUP ID 相同的用户信息
[root@localhost ~]# awk -F ':' '$3 == $4' /etc/passwd root:x:0:0:young,geek,010110110,0101101101:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin abrt:x:173:173::/etc/abrt:/sbin/nologin