能够打印报表
有多种版本:New awk(nawk),GNU awk( gawk)
[root@centos7 ~]#ll `which awk`
lrwxrwxrwx. 1 root root 4 Mar 29 12:06 /usr/bin/awk -> gawk #系统安装gawk
awk [options] ‘program’ file
-
options(参数):
1.1. -F “分隔符” 指明输入时用到的字段分隔符(不写默认空白符为分隔符)
1.2. -v var=value 变量赋值 -
program(程序):pattern{action statements;…},通常放在单引号中
2.1. pattern:BEGIN(打印表头),END(实现统计)
awk [options] ‘BEGIN{action;… }pattern{action;… }END{action;… }’ file
2.2. action:print,printf
分隔符:
- awk执行时,由分隔符分隔的字段(域)标记$1,$2…$n称为域标识。$0为所有域,注意:此时和shell中变量$符含义不同
- 文件的每一行称为记录
- 省略action,则默认执行 print $0 的操作
[root@centos7 ~]#awk '{print $1}' /etc/fstab
####options(-F)没有,默认以空白符为分隔符(不分多少),打印第一段
#
#
#
#
#
#
#
UUID=b640a874-b15b-41d1-acc3-eb2a4e85ac9e
[root@centos7 ~]#awk '{print hello}' /etc/fstab
#####/etc/fstab有多少行打印多少行hello,跟/etc/fstab内容没有关系
[root@centos7 ~]#awk '{print "hello"}' /etc/fstab
hello
hello
hello
hello
#####字符串时需加双引号,否则会识别成变量,数字可以不加双引号
[root@centos7 ~]#awk '{print 100*20}' /etc/fstab
2000
2000
2000
2000
2000
[root@centos7 ~]#awk 'BEGIN{print 100*20}' #打印第一行(表头)
2000
[root@centos7 ~]#awk 'BEGIN{print hello}' #引号区别
[root@centos7 ~]#awk 'BEGIN{print "hello"}'
hello
[root@centos7 ~]#awk 'BEGIN{print "number"}{print 100*20}'/etc/fstab #打印表头
number
2000
2000
2000
[root@centos7 ~]#awk 'BEGIN{print "number"}{print 100*20}END{print "end"}' /etc/fstab #打印表头表尾
number
2000
2000
2000
end
df取利用率
[root@centos7 ~]#df |tr -s " "|cut -d" " -f5 |cut -d% -f1 |sort -nr |head -n1
8
[root@centos7 ~]#df |tr -s " " %|cut -d% -f5 |sort -nr |head -n1
8
[root@centos7 ~]#df |egrep -o '[0-9]+%' |grep -o '[0-9]' |sort -nr|head -n1
8
[root@centos7 ~]#df |sed -rn 's/.*([0-9]+)%.*/\1/p' |sort -nr |head -n1
8
[root@centos7 ~]#df |awk '{print $5}'|cut -d% -f1 |sort -nr |head -n1
8
[root@centos7 ~]#df |awk '{print $5}' |awk -F% '{print $1}' |sort -nr |head -n1
8
[root@centos7 ~]#df |awk -F " +|%" '{print $5}'|sort -nr |head -n1
8
access_log取最多IP
[root@localhost ~]#cat access_log |head -n1
172.18.118.91 - - [20/May/2018:08:09:59 +0800] "GET / HTTP/1.1" 200 912 "-" "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1; Trident/5.0)"
[root@localhost ~]#awk '{print $1}' access_log |sort |uniq -c |sort -nr |head -n3
4870 172.20.116.228
3429 172.20.116.208
2834 172.20.0.222
[root@localhost ~]#awk -F "[[ ]" '{print $5}' access_log |head -n2
20/May/2018:08:09:59
20/May/2018:08:09:59
[root@localhost ~]#awk -F "[[ ]" '{print $1,$5}' access_log |head -n2 $1,$5不填写时默认空格隔开,填写其他隔开符需加双引号
172.18.118.91 20/May/2018:08:09:59
172.18.118.91 20/May/2018:08:09:59
[root@localhost ~]#awk -F "[[ ]" '{print $1"++"$5}' access_log |head -n2
172.18.118.91++20/May/2018:08:09:59
172.18.118.91++20/May/2018:08:09:59
[root@localhost ~]#awk -F "[[ ]" '{print $1"--"$5}' access_log |head -n2
172.18.118.91--20/May/2018:08:09:59
[root@localhost ~]#awk -F "[[ ]" '{print $1"\t"$5}' access_log |head -n2 #\t(tab键)分隔符,能自动对齐
172.18.118.91 20/May/2018:08:09:59
172.18.118.91 20/May/2018:08:09:59
passwd
[root@localhost ~]#awk -F: '{print $1}' /etc/passwd |head -n2
root
bin
###取最后一列
[root@localhost ~]#awk -F: '{print $7}' /etc/passwd |head -n2 #知道每行多少列且列数一样
/bin/bash
/sbin/nologin
[root@localhost ~]#cat /etc/passwd |rev |awk -F: '{print $1}' |rev |head -n2
/bin/bash
/sbin/nologin
[root@localhost ~]#awk -F: '{print $NF}' /etc/passwd |head -n2 #不知道列数,引用变量
/bin/bash
/sbin/nologin
变量
变量:内置和自定义变量
-v 变量
FS(输入字段分隔符,默认为空白字符)
[root@localhost ~]#awk -F: '{print $1,$3}' /etc/passwd |head -n2
root 0
bin 1
[root@localhost ~]#awk -v FS=":" '{print $1,$3}' /etc/passwd |head -n2 #与前面效果一样,但当输出分隔符为变量时方便
root 0
bin 1
[root@localhost ~]#awk -v FS=":" '{print $1FS$3}' /etc/passwd |head -n2 #引用变量为分隔符
root:0
bin:1
[root@localhost ~]#awk -v FS=":" '{print $1":"$3}' /etc/passwd |head -n2
root:0
bin:1
[root@localhost ~]#fs=:;awk -v FS=$fs '{print $1FS$3}' /etc/passwd |head -n2
root:0
bin:1
OFS(输出字段分隔符,默认为空白字符)
[root@localhost ~]#awk -F "[[ ]" '{print $1,$5}' access_log |head -n1
172.18.118.91 20/May/2018:08:09:59
[root@localhost ~]#awk -F "[[ ]" -v OFS=: '{print $1,$5}' access_log |head -n1
172.18.118.91:20/May/2018:08:09:59
[root@localhost ~]#awk -F "[[ ]" -v OFS=++ '{print $1,$5}' access_log |head -n1
172.18.118.91++20/May/2018:08:09:59
[root@localhost ~]#awk -F "[[ ]" -v OFS=-- '{print $1,$5}' access_log |head -n1
172.18.118.91--20/May/2018:08:09:59
RS(输入记录分隔符,指定输入时的换行符)
默认记录符为换行符,以记录符之间内容为一行
[root@localhost ~]#cat awk.txt
a,b,c
d;e,f
ggg;hhhh;xxx
yyy
[root@localhost ~]#awk -F, -v RS=";" '{print $1}' awk.txt
a
e
hhhh
xxx
yyy
[root@localhost ~]#awk -F, -v RS=";" '{print $3}' awk.txt
c
d
ORS(:输出记录分隔符,输出时用指定符号代替换行符)
[root@localhost ~]#awk -F: -v ORS=" " '{print $1,$3}' /etc/passwd
root 0 bin 1 daemon 2 adm 3 lp 4 sync 5 shutdown 6 halt 7 mail 8 operator 11 games 12 ftp 14 nobody 99 systemd-network 192 dbus 81 polkitd 999 sssd 998 libstoragemgmt 997 colord 996 rpc 32 gluster 995 saslauth 994 abrt 173 setroubleshoot 993 rtkit 172 radvd 75 chrony 992 qemu 107 unbound 991 ntp 38 tss 59 usbmuxd 113 geoclue 990 pulse 171 gdm 42 saned 989 rpcuser 29 nfsnobody 65534 gnome-initial-setup 988 sshd 74 avahi 70 postfix 89 tcpdump 72 wang 1000 apache 48 mandriva 1005 slackware 2002 [root@localhost ~]#
NF(字段数量)
[root@localhost ~]#awk -F: '{print NF}' /etc/passwd |