Awk 变量以字母开头,后续字符可以是数字、字母、或下划线。关键字不能用作 awk 变量。awk 变量可以直接使用而不需事先声明。 如果要初始化变量,最好在BEGIN 区域内做,它只会执行一次。Awk 中没有数据类型的概念,一个 awk 变量是 number 还是 string 取决于该变量所处的上下文。
实例1:使用”total”便是用户建立的用来存储公司所有雇员工资总和的变量。
[root@localhost ~]# cat emp4
101,John 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
[root@localhost ~]# cat emp.awk
BEGIN{
FS=",";
total=0;
}
{
print $2 "‘s salary is:" $4;
total=total+$4;
}
END{
print "---\nTotal company salary=$"total;
}
[root@localhost ~]# awk -f emp.awk emp4
John Doe‘s salary is:10000
Jason Smith‘s salary is:5000
Raj Reddy‘s salary is:4500
Anand Ram‘s salary is:4500
Jane Miller‘s salary is:3000
---
Total company salary=$27000
1.取正取反
只接受单个操作数的操作符叫做一元操作符。
实例1:取反操作
[root@localhost ~]# cat emp4
101,John 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
[root@localhost ~]# awk -F, ‘{print -$4}‘ emp4
-10000
-5000
-4500
-4500
-3000注意:取反只对数值类数据生效,字符串取反结果全部为0.
实例2:
[root@localhost ~]# cat num
-1
-2
-3
[root@localhost ~]# awk ‘{print +$1}‘ num
-1
-2
-3
[root@localhost ~]# awk ‘{print -$1}‘ num
1
2
32.自增自减
VAR1=++VAR或者VAR1=--VAR,表示VAR先增加或者减去1再赋值给VAR1,VAR1=VAR++或VAR1=VAR--,表示先将VAR赋值给VAR1,VAR再增减或者减去1.
实例1:前自加子减
[root@localhost ~]# awk -F, ‘{print ++$4}‘ emp4 #前自加
10001
5001
4501
4501
3001
[root@localhost ~]# awk -F, ‘{print --$4}‘ emp4 #前自减
9999
4999
4499
4499实例2:后自加子减
[root@localhost ~]# awk -F, ‘{$4--;print $4}‘ emp4 #后自减
9999
4999
4499
4499
2999
[root@localhost ~]# awk -F, ‘{$4++;print $4}‘ emp4 #后自加
10001
5001
4501
4501
3001实例3:打印所有可登陆 shell 的用户总数:
[root@localhost ~]# awk -F‘:‘
>‘$NF ~ /\/bin\/bash/ { n++ }
> END { print n }‘ /etc/passwd
10
[root@localhost ~]# grep -c ‘/bin/bash$‘ /etc/passwd
10实例4:
[root@localhost ~]# cat num
1
2
1
1
3
4
2
[root@localhost ~]# awk ‘/1/{print NF}‘ num
1
1
1
[root@localhost ~]# awk ‘/1/{n++}END{print n}‘ num
3
[root@localhost ~]# awk ‘/2/{n++}END{print n}‘ num
2
[root@localhost ~]# awk ‘/3/{n++}END{print n}‘ num
1
[root@localhost ~]# awk ‘/4/{n++}END{print n}‘ num
1需要两个操作数的操作符,成为二元操作符。 Awk 中有多种基本二元操作符(如算术操作符、 字符串操作符、赋值操作符,等等)。

实例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
实例9:打印/etc/passwd 中 UID >= 100 并且用户的 shell 是/bin/sh 的用户
[root@localhost ~]# awk -F: ‘$3 >= 100 && $7 == "/bin/sh"‘ /etc/passwd user1:x:800:800:test user:/none:/bin/sh
或者:
[root@localhost ~]# awk -F ‘:‘ ‘$3 >= 100 && $NF ~ /\/bin\/sh/‘ /etc/passwd user1:x:800:800:test user:/none:/bin/sh #正则表达式模式匹配
实例10:打印/etc/passwd 中没有注释信息(第 5 个字段)的用户
[root@localhost ~]# awk -F: ‘$5 == ""‘ /etc/passwd abrt:x:173:173::/etc/abrt:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin sys:x:498:1001::/home/sys:/bin/bash natasha:x:1006:1007::/home/natasha:/bin/bash harry:x:1007:1008::/home/harry:/bin/bash sarah:x:497:497::/home/sarah:/bin/nologin

实例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 ~]# awk -F, ‘$2 == "Tennis"‘ items.txt #精确匹配 [root@localhost ~]# awk -F, ‘$2 ~ "Tennis"‘ items.txt #模糊匹配 104,Tennis Racket,Sports,190,20
实例2:
[root@localhost ~]# awk -F, ‘$2 !~ "Tennis"‘ items.txt #不匹配 101,HD Camcorder,Video,210,10 102,Refrigerator,Appliance,850,2 103,MP3 Player,Audio,270,15 105,Laser Printer,Office,475,5
实例3:打印 shell 为/bin/bash 的用户的总数,如果最后一个字段包含”/bin/bash”,则变量n 增加 1
[root@localhost ~]# grep -c ‘/bin/bash‘ /etc/passwd
9
[root@localhost ~]# awk -F: ‘$NF ~ /\/bin\/bash/ { n++ } END{ print n}‘ /etc/passwd
9本文出自 “追求不完美” 博客,请务必保留此出处http://yolynn.blog.51cto.com/11575833/1891219
原文:http://yolynn.blog.51cto.com/11575833/1891219