一、运算符
= += -= *= /= %= ^= **= | 赋值 |
?: | C条件表达式 |
|| | 逻辑或 |
&& | 逻辑与 |
~ 和 !~ | 匹配正则表达式和不匹配正则表达式 |
< <= > >= != == | 关系运算符 |
空格 | 连接 |
+ - | 加,减 |
* / % | 乘,除与求余 |
+ - ! | 一元加,减和逻辑非 |
^ *** | 求幂 |
++ -- | 增加或减少,作为前缀或后缀 |
$ | 字段引用 |
in | 数组成员 |
二、示例
#过滤第一列大于2的行
[root@localhost ~]# awk '$1>2' log.txt
3 Are you like awk
This's a test
10 There are orange,apple,mongo
#过滤第一列等于2的行
[root@localhost ~]# awk '$1==2 {print $1,$3}' log.txt
2 is
#过滤第一列大于2并且第二列等于'Are'的行
[root@localhost ~]# awk '$1>2 && $2=="Are" {print $1,$2,$3}' log.txt
3 Are you
[root@localhost ~]# awk -F: '$3 == 0' passwd.txt
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# awk -F: '$3 < 10' passwd.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@localhost ~]# awk -F: '$3 < 5' passwd.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost ~]# awk -F: '$NF == "/bin/bash"' passwd.txt
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# awk -F: '$1 == "root"' passwd.txt
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# awk -F: '$1 ~ /roo/' passwd.txt
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# awk -F: '$1 !~ /roo/' passwd.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin10
[root@localhost ~]# df -P | grep '/' | awk '$4 > 25000'
devtmpfs 485816 0 485816 0% /dev
tmpfs 497836 0 497836 0% /dev/shm
tmpfs 497836 13968 483868 3% /run
tmpfs 497836 0 497836 0% /sys/fs/cgroup
/dev/mapper/centos-root 6486016 1898056 4587960 30% /
/dev/sda1 1038336 140336 898000 14% /boot
tmpfs 99568 0 99568 0% /run/user/0