9.6 awk(上)
封装josnp框架、测试封装函数
支持分段:
-F:选择分隔符。默认以空格或者是空白字符作为分隔符。
print:选择打印第几段,可以同时指定打印多段,用“,”逗号分隔。$0代表所有。
-
[root@localhost awk]# awk -F ':' '{print $1}' passwd
-
root
-
bin
-
daemon
-
adm
-
lp
-
sync
-
shutdown
-
halt
-
mail
-
operator
多段的输出指定间隔符号,默认空格:
-
[root@localhost awk]# awk -F ':' '{print $1,$6}' passwd | head -2
-
root /root
-
bin /bin
-
[root@localhost awk]# awk -F ':' '{print $1" # "$6}' passwd | head -2
-
root # /root
-
bin # /bin
类似grep的匹配功能:
-
[root@localhost awk]# awk '/oo/' passwd
-
root:x:0:0:root:/root:/bin/bash
-
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
-
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
-
operator:x:11:0:operator:/root:/sbin/nologin
-
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
-
[root@localhost awk]# awk -F ':' '$1 ~ /oo/' passwd //第一行包含的匹配的内容。
-
root:x:0:0:root:/root:/bin/bash
多功能匹配:
-
[root@localhost awk]# awk -F ':' '/root/ {print $1,$3} /user/ {print $1,$3}' passwd
-
root 0
-
operator 11
-
user2 1001
-
user3 1004
-
user4 1006
-
user5 1008
-
saslauth 997
-
[root@localhost awk]# grep -E 'root|user' passwd
-
root:x:0:0:root:/root:/bin/bash
-
operator:x:11:0:operator:/root:/sbin/nologin
-
user2:x:1001:1001::/home/user2:/bin/bash
-
user3:x:1004:1004::/home/user3:/bin/bash
-
user4:x:1006:1005::/home/user4:/bin/bash
-
user5:x:1008:1008::/home/user5:/bin/bash
-
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
-
或者还可以:
-
[root@localhost awk]# awk -F ':' '/root|user/ {print $0}' passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
user2:x:1001:1001::/home/user2:/bin/bash
user3:x:1004:1004::/home/user3:/bin/bash
user4:x:1006:1005::/home/user4:/bin/bash
user5:x:1008:1008::/home/user5:/bin/bash
saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin
针对数学运算表达式的
-
[root@localhost awk]# awk -F ':' '$3==0' passwd
-
root:x:0:0:root:/root:/bin/bash
-
[root@localhost awk]# awk -F ':' '$3==0 {print $0}' passwd
-
root:x:0:0:root:/root:/bin/bash
-
[root@localhost awk]# awk -F ':' '$3>=1000 {print $0}' passwd //如果这里数字使用双引号括起来,那么是使用ASCII码的方式进行匹配/对比。所以如果是对比数字,那么就不要双引号。
xiao:x:1000:1000::/home/xiao:/bin/bash
user2:x:1001:1001::/home/user2:/bin/bash
user3:x:1004:1004::/home/user3:/bin/bash
user4:x:1006:1005::/home/user4:/bin/bash
haha:x:1007:1005::/home/test:/sbin/nologin
user5:x:1008:1008::/home/user5:/bin/bash
test1:x:1100:1009::/home/test1:/bin/bash
zhangsan:x:1101:1101::/home/zhangsan:/bin/bash
aiya:x:1102:1102::/www/aiya:/sbin/nologin
9.7 awk(下)
取反:匹配内容不为什么的:
-
[root@localhost awk]# awk -F ':' '$7!="/sbin/nologin" {print $0}' passwd
-
root:x:0:0:root:/root:/bin/bash
-
sync:x:5:0:sync:/sbin:/bin/sync
-
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
-
halt:x:7:0:halt:/sbin:/sbin/halt
-
xiao:x:1000:1000::/home/xiao:/bin/bash
-
user2:x:1001:1001::/home/user2:/bin/bash
-
user3:x:1004:1004::/home/user3:/bin/bash
-
user4:x:1006:1005::/home/user4:/bin/bash
-
user5:x:1008:1008::/home/user5:/bin/bash
-
test1:x:1100:1009::/home/test1:/bin/bash
-
zhangsan:x:1101:1101::/home/zhangsan:/bin/bash
阅读(1106) | 评论(0) | 转发(0) |