一,比较表达式
1,
用来对文本作比较,只有条件为真,才执行指定的动作
< gawk '$3 < 500' /etc/passwd #系统账户
< = gawk '$3 < = 499' /etc/passwd #同上
= = gawk '$3 = = 0' /etc/passwd #id为0,root账户
!= gawk '$3 != 500' /etc/passwd #非id=500的账户
> = gawk '$3 > = 500' /etc/passwd #普通账户
> gawk '$3 > 499' /etc/passwd #同上
~ gawk '$1 ~ /^root' /etc/passwd #root账户
~ gawk '$1 !~ /^root' /etc/passwd #非root账户
2,
[root@rhel helinbash]# gawk -F: '$3 < 500{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
gopher
ftp
nobody
nscd
vcsa
rpc
mailnull
smmsp
pcap
ntp
dbus
avahi
sshd
rpcuser
haldaemon
avahi-autoipd
xfs
gdm
sabayon
named
[root@rhel helinbash]#
3,
[root@rhel helinbash]# gawk -F: '$3 <= 499{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
news
uucp
operator
games
gopher
ftp
nobody
nscd
vcsa
rpc
mailnull
smmsp
pcap
ntp
dbus
avahi
sshd
rpcuser
haldaemon
avahi-autoipd
xfs
gdm
sabayon
named
[root@rhel helinbash]#
4,
[root@rhel helinbash]# gawk -F: '$3 > 499{print $1}' /etc/passwd
nfsnobody
oracle
[root@rhel helinbash]#
5,
[root@rhel helinbash]# gawk -F: '$3 == 0 {print $1}' /etc/passwd
root
[root@rhel helinbash]#
二,条件表达式
1,
格式:条件表达式1 ? 表达式2 :表达式3与之等同的代码段如下
2,
[root@rhel helinbash]# gawk -F: '{ printf("%s is %s user\n",$1,($3 >= 500? "Normal": "system"))}' /etc/passwd
root is system user
bin is system user
daemon is system user
adm is system user
lp is system user
sync is system user
shutdown is system user
halt is system user
mail is system user
news is system user
uucp is system user
operator is system user
games is system user
gopher is system user
ftp is system user
nobody is system user
nscd is system user
vcsa is system user
rpc is system user
mailnull is system user
smmsp is system user
pcap is system user
ntp is system user
dbus is system user
avahi is system user
sshd is system user
rpcuser is system user
nfsnobody is Normal user
haldaemon is system user
avahi-autoipd is system user
xfs is system user
gdm is system user
sabayon is system user
oracle is Normal user
named is system user
[root@rhel helinbash]#
三,POSIX字符类表达式
1,
[[:allnum:]] 字母和数字字符
[[:alpha:]] 字母字符等同于
[[:cntrl:]] 控制字符
[[:digit:]] 数字字符
[[:graph:]] 非空白字符(空格,控制字符等)
[[:lower:]] [[:upper:]] 小写/大写字母
[[:space:]] 所有的空白字符(换行符,空格,制表符)
2,
[root@rhel helinbash]# gawk -F: '/^[[:digit:]]/' /etc/inittab
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
[root@rhel helinbash]#
3,
[root@rhel helinbash]# gawk -F: '/^[[:alpha:]]/' /etc/inittab
id:5:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
x:5:respawn:/etc/X11/prefdm -nodaemon
[root@rhel helinbash]#
4,
[root@rhel helinbash]# gawk -F: '/^[A-Z]/' /etc/inittab #这种方式不区分大小写
id:5:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
x:5:respawn:/etc/X11/prefdm -nodaemon
[root@rhel helinbash]#
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29611940/viewspace-1174360/
,如需转载,请注明出处,否则将追究法律责任。