awk

Comparison Operators

awk支持标准的比较操作符列表如下:

Operator Description
> Is greater than
>= Is greater than or equal to
< Is less than
<= Is less than or equal to
== Is equal to
!= Is not equal to
&& Both the conditional expressions are true
Either one of the conditional expressions is true

在下面的例子中的注意事项:如果你不指定任何动作,awk将print整条记录,如果它匹配条件表达式。 下面的示例命名用<=条件表达式。这将显示所有小于5临界水平的项目:

$ awk -F "," '$5<=5' items.txt
102,Refrigerator,Appliance,850,2
105,Laser Printer,Office,475,5

下面的例子使用==条件。显示项目号为103的记录

$ awk -F "," '$1 == 103' items.txt
103,MP3 Player,Audio,270,15

注意:不要混淆==(精确匹配)和 = (赋值)

仅输出103号条目描述:

$ awk -F "," '$1 == 103 {print $2}' items.txt
MP3 Player

下面的列子使用!=条件。这将打印所有项目,除了那些Video分类:

$ 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

同上,但只打印项目描述:

$ awk -F "," '$3 != "Video" {print $2}' items.txt
Refrigerator
MP3 Player
Tennis Racket
Laser Printer

下面示例使用&&(与运算符)来检查两个条件。此打印记录价格小于900并且数量小于或等于5

$ awk -F "," '$4 < 900 && $5 <= 5' items.txt
102,Refrigerator,Appliance,850,2
105,Laser Printer,Office,475,5

同上,但是仅打印项目描述:

$ awk -F "," '$4 < 900 && $5 <= 5 {print $2}' items.txt
Refrigerator
Laser Printer

下面的示例使用||(逻辑或)去检查两个条件。这打印价格小于900或数量小于等于5记录。

$ awk -F "," '$4 < 900 || $5 <= 5' 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

同上,但是只打印项目描述:

$ awk -F "," '$4 < 900 || $5 <= 5 {print $2}' items.txt
HD Camcorder
Refrigerator
MP3 Player
Tennis Racket
Laser Printer

下面示例使用>(大于)条件。这个例子显示/etc/passwd中具有最高的用户id值的UID(和全行)。这个脚本跟踪最大数(字段3)在变量maxuid里,并保持相应的复制行在变量maxline里。一旦它循环所有行,它打印uid和line.

awk -F ":" '$3 > maxuid { maxuid=$3; maxline=$0}; END {print maxuid, maxline }' /etc/passwd
505 yqd_jack:x:505:50::/data/www/www.0550go.com:/sbin/nologin

下面的示例使用==条件。这个例子打印/etc/passwd文件含有相同用户id和组id的每一行。这个awk脚本如果$3(用户id)与$4(组id)相等,就打印该行。

$ awk -F ":" '$3==$4' /etc/passwd
root:x:0:0:root:/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
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
mysql:x:500:500::/home/mysql:/bin/bash
zabbix:x:501:501::/home/zabbix:/sbin/nologin
luckyzhou:x:504:504::/home/luckyzhou:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin
mongod:x:498:498:mongod:/var/lib/mongo:/bin/false

下面的示例使用 >= 和 && 条件。这个例子打印/etc/passwd里当user id>=100并且用户shell是/bin/bash的行。

$ awk -F ":" '$3>=100 && $NF ~/\/bin\/bash/' /etc/passwd
mysql:x:500:500::/home/mysql:/bin/bash
luckyzhou:x:504:504::/home/luckyzhou:/bin/bash

下面示例使用==条件。这个例子打印/etc/passwd里不含有一个评论(字段5)所有的行

$ awk -F ":" '$5 == ""' /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
mysql:x:500:500::/home/mysql:/bin/bash
zabbix:x:501:501::/home/zabbix:/sbin/nologin
jack:x:502:50::/data/jianlin/:/sbin/nologin
varnish:x:503:502::/home/varnish:/sbin/nologin
luckyzhou:x:504:504::/home/luckyzhou:/bin/bash
yqd_jack:x:505:50::/data/www/www.0550go.com:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin