一、awk概述
“奥克”
awk:报告生成器,格式化打印
把文件中读取到的每一行的每一个字段分别进行格式化,而后进行显示
内部支持变量、条件判断、循环、数组
awk --> new awk --> nawk(收费)
GNU awk --> gawk "告克"
二、awk的使用
awk和默认以任意个空白符为分隔符,而cut默认的是制表符,而且只能指定以一个空格符为分隔符
注意:与sed一样使用‘’不要使用""容易报错
用法格式:
awk [OPTIONS] ‘SCRIPT‘ FILE...
awk [OPTIONS] ‘pattern/{action}‘ File...
action:
print,默认打印
pattern:
包括地址定界:
/pat1/,/pat2/
/pattern/
!/pattern
expression:表达式:>,<,=,>=,<=,=,!=,~
BEGIN:执行前的准备工作
END:执行后的收尾工作
awk常用的四种分隔符:
输入:
行分隔符:默认换行符:“$",cat -A可以查看文件的所有空白字符
字段分隔符:默认制表符
输出:
行分隔符:默认换行符:“$"
字段分隔符:空白
awk的内置变量:
$1,$2,$3...:第2个字段,第2个字段,第3个字段...
$0:表示整行
$NF:表示最后一个字段, NF:表示总字段数
FS:读入行时使用的字段分隔符.默认空白
OFS:输出行时使用的字段分隔符.默认空白
[[email protected] shell]# awk -F : ‘/^root/ {print $1}‘ /etc/passwd root [[email protected] shell]# awk -F: ‘/^\<root\>/ {print $1}‘ /etc/passwd root [[email protected] shell]# awk -F : ‘/^root/ {print $1,$7}‘ /etc/passwd #逗号只是命令的一部分,不是输出的结果 root /bin/bash [[email protected] shell]# awk -F : ‘/^root/ {print $1$7}‘ /etc/passwd root/bin/bash [[email protected] shell]# awk -F: ‘/^\<root\>/ {print username $1,$7}‘ /etc/passwd root /bin/bash [[email protected] shell]# awk -F: ‘/^\<root\>/ {print username sb$1,$7}‘ /etc/passwd root /bin/bash [[email protected] shell]# awk -F: ‘/^\<root\>/ {print username sb $1,$7}‘ /etc/passwd root /bin/bash [[email protected] shell]# awk -F: ‘/^\<root\>/ {print "username sb" $1,$7}‘ /etc/passwd username sbroot /bin/bash [[email protected] shell]# awk -F: ‘/^\<root\>/ {print "username sb " $1,$7}‘ /etc/passwd username sb root /bin/bash [[email protected] shell]# ^C [[email protected] shell]# awk -F: ‘/^\<root\>/ {print "username sb " $1,nihao$7}‘ /etc/passwd username sb root /bin/bash [[email protected] shell]# awk -F: ‘/^\<root\>/ {print "username sb " $1,"nihao"$7}‘ /etc/passwd username sb root nihao/bin/bash [[email protected] shell]# awk -F: ‘/^\<root\>/ {print "username sb " $1,"nihao"$7sb}‘ /etc/passwd username sb root nihao/bin/bash [[email protected] shell]# awk -F: ‘/^\<root\>/ {print "username sb " $1,"nihao"$7"sb"}‘ /etc/passwd username sb root nihao/bin/bashsb [[email protected] shell]# awk -F: ‘/^\<root\>/ {print "username sb " $1,"nihao"$7"\nsb"}‘ /etc/passwd username sb root nihao/bin/bash sb
[[email protected] shell]# df -h|awk ‘{print $1}‘ Filesystem /dev/sda2 tmpfs /dev/sda1 /dev/sda5 [[email protected] shell]# df -h|awk ‘{print $2}‘ Size 29G 495M 477M 68G [[email protected] shell]# df -h|awk "{print $2}" Filesystem Size Used Avail Use% Mounted on /dev/sda2 29G 4.6G 23G 17% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 477M 28M 424M 7% /boot /dev/sda5 68G 4.3G 60G 7% /usr [[email protected] shell]# df -h|awk ‘{print}‘ Filesystem Size Used Avail Use% Mounted on /dev/sda2 29G 4.6G 23G 17% / tmpfs 495M 0 495M 0% /dev/shm /dev/sda1 477M 28M 424M 7% /boot /dev/sda5 68G 4.3G 60G 7% /usr
1、取/etc/passwd中uid大于500的用户名
[[email protected] shell]# awk -F: ‘$3>=500{print "Username: "$1}‘ /etc/passwd Username: testbash Username: basher Username: nologin Username: fedora Username: user1 Username: bash1 Username: xj Username: Oracle Username: bash Username: xxj Username: openstack Username: centos
2、取/etc/passwd中以bash结尾的行
[[email protected] shell]# awk -F: ‘$7~/bash$/{print}‘ /etc/passwd root:x:0:0:chfn root:/root:/bin/bash testbash:x:501:501::/home/testbash:/bin/bash basher:x:502:502::/home/basher:/bin/bash fedora:x:504:504::/home/fedora:/bin/bash user1:x:505:505::/home/user1:/bin/bash bash1:x:506:506::/home/bash1:/bin/bash xj:x:507:507::/home/xj:/bin/bash Oracle:x:3000:3000::/home/database:/bin/bash bash:x:3001:3001::/home/bash:/bin/bash xxj:x:3002:3002::/home/xxj:/bin/bash openstack:x:3003:3003::/home/openstack:/bin/bash centos:x:3004:3004::/home/centos:/bin/bash [[email protected] shell]# awk -F: ‘/bash$/{print "sbsb: "$0}‘ /etc/passwd sbsb: root:x:0:0:chfn root:/root:/bin/bash sbsb: testbash:x:501:501::/home/testbash:/bin/bash sbsb: basher:x:502:502::/home/basher:/bin/bash sbsb: fedora:x:504:504::/home/fedora:/bin/bash sbsb: user1:x:505:505::/home/user1:/bin/bash sbsb: bash1:x:506:506::/home/bash1:/bin/bash sbsb: xj:x:507:507::/home/xj:/bin/bash sbsb: Oracle:x:3000:3000::/home/database:/bin/bash sbsb: bash:x:3001:3001::/home/bash:/bin/bash sbsb: xxj:x:3002:3002::/home/xxj:/bin/bash sbsb: openstack:x:3003:3003::/home/openstack:/bin/bash sbsb: centos:x:3004:3004::/home/centos:/bin/bash [[email protected] shell]# awk -F: ‘$7=="/bin/bash" {print $1}‘ /etc/passwd root testbash basher fedora user1 bash1 xj Oracle bash xxj openstack centos
BEGIN,END:
[[email protected] shell]# awk -F: ‘$3>=500 {print "-------------\n"$1}‘ /etc/passwd ------------- testbash ------------- basher ------------- nologin ------------- fedora ------------- user1 ------------- bash1 ------------- xj ------------- Oracle ------------- bash ------------- xxj ------------- openstack ------------- centos [[email protected] shell]# awk -F: ‘BEGIN{print "Username\n----------"}$3>=500 {print $1}‘ /etc/passwd Username ---------- testbash basher nologin fedora user1 bash1 xj Oracle bash xxj openstack centos [[email protected] shell]# awk -F: ‘$3>=500 {print $1}END{print "-------"}‘ /etc/passwd testbash basher nologin fedora user1 bash1 xj Oracle bash xxj openstack centos ------- [[email protected] shell]# awk ‘BEGIN{FS=":"} $3>=500 {print $7} ‘ /etc/passwd /bin/bash #用BEGIN指定分隔符 /bin/bash /sbin/nologin /bin/bash /bin/bash /bin/bash /bin/bash /bin/bash /bin/bash /bin/bash /bin/bash /bin/bash
练习:
1、显示GID小于500的组
# awk -F: ‘$3<500{print $1}‘
2、显示默认shell为nologin的用户
# awk -F: ‘/\<nologin$/ {print $1}‘
# awk -F: ‘$7=="/sbin/nologin" {print $1}‘
3、显示eh0网卡文件的配置信息,注意只显示=后面的值
# awk -F= ‘{print $2}‘
4、显示/etc/sysctl.conf文件中定义的内核参数,只显示名称
5、显示eth0网卡的ip地址,通过Ifconfig的命令结果进行过滤
# ifconfig eth0|awk ‘/inet addr/{print}‘|awk -F: ‘{print $2}‘|awk ‘{print $1}‘