awk常见选项
-F "分隔符"指明输入时用到的字段分隔符,默认的分隔符是若干个连续空白符
-v var=value 变量赋值
动作print
格式:
print item1,item2,...
说明:
逗号分隔符
输出item可以字符串,也可是数值;当前记录的字段、变量或awk的表达式
如省略item,相当于print $0
固定字符需要用 ""引起来,而变量和数字不需要
1、取出用UUID挂载的挂载点和磁盘文件系统
[root@y_zilong ~]# cat /etc/fstab
/dev/mapper/cl-root / xfs defaults 0 0
UUID=b3690942-53e2-45a6-a459-3e23ab4d45f3 /boot ext4 defaults 1 2
/dev/mapper/cl-swap swap swap defaults 0 0
[root@y_zilong ~]# grep "^UUID" /etc/fstab
UUID=b3690942-53e2-45a6-a459-3e23ab4d45f3 /boot ext4 defaults 1 2
[root@y_zilong ~]# grep "^UUID" /etc/fstab |awk '{print $2,$3}'
/boot ext4
[root@y_zilong ~]#
2、取出网站访问量最大的前3个IP
[root@y_zilong ~]# cat /var/log/nginx/access.log
10.0.0.1 - - [22/Apr/2021:04:44:02 -0400] "GET / HTTP/1.1" 200 4057 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-"
10.0.0.1 - - [22/Apr/2021:04:44:02 -0400] "GET /nginx-logo.png HTTP/1.1" 200 368 "http://10.0.0.133/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-"
10.0.0.1 - - [22/Apr/2021:04:44:02 -0400] "GET /poweredby.png HTTP/1.1" 200 4148 "http://10.0.0.133/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-"
10.0.0.1 - - [22/Apr/2021:04:44:02 -0400] "GET /favicon.ico HTTP/1.1" 404 3971 "http://10.0.0.133/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-"
10.0.0.1 - - [22/Apr/2021:04:44:13 -0400] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36" "-"
[root@y_zilong ~]# awk '{print $1}' /var/log/nginx/access.log
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
10.0.0.1
[root@y_zilong ~]# awk '{print $1}' /var/log/nginx/access.log |sort|uniq -c|sort -nr|head -3
5 10.0.0.1
[root@y_zilong ~]#
3、取nginx的访问日志的中IP和时间
[root@y_zilong ~]# awk -F'[[ ]' '{print $1,$5}' /var/log/nginx/access.log |head -3
10.0.0.1 22/Apr/2021:04:44:02
10.0.0.1 22/Apr/2021:04:44:02
10.0.0.1 22/Apr/2021:04:44:02
[root@y_zilong ~]#
4、取出分区利用率
[root@y_zilong ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 909016 0 909016 0% /dev
tmpfs 924732 0 924732 0% /dev/shm
tmpfs 924732 9888 914844 2% /run
tmpfs 924732 0 924732 0% /sys/fs/cgroup
/dev/mapper/cl-root 17811456 4158052 13653404 24% /
/dev/sda1 999320 135708 794800 15% /boot
tmpfs 184944 28 184916 1% /run/user/42
tmpfs 184944 2344 182600 2% /run/user/0
/dev/sr0 6967726 6967726 0 100% /run/media/root/CentOS-8-BaseOS-x86_64
[root@y_zilong ~]# df |awk '{print $1,$5}'
Filesystem Use%
devtmpfs 0%
tmpfs 0%
tmpfs 2%
tmpfs 0%
/dev/mapper/cl-root 24%
/dev/sda1 15%
tmpfs 1%
tmpfs 2%
/dev/sr0 100%
[root@y_zilong ~]# df |awk -F"[[:space:]]+|%" '{print $1,$5}'
Filesystem Use
devtmpfs 0
tmpfs 0
tmpfs 2
tmpfs 0
/dev/mapper/cl-root 24
/dev/sda1 15
tmpfs 1
tmpfs 2
/dev/sr0 100
[root@y_zilong ~]# df |grep "^/dev/sd"|awk -F"[[:space:]]+|%" '{print $1,$5}'
/dev/sda1 15
[root@y_zilong ~]# df |awk -F"[[:space:]]+|%" '/^\/dev\/sd/{print $5}'
15
[root@y_zilong ~]# df |awk -F"[[:space:]]+|%" '/^\/dev\/sd/{print $1,$5}'
/dev/sda1 15
[root@y_zilong ~]# df |awk -F' +|%' '/^\/dev\/sd/{print $1,$5}'
/dev/sda1 15
[root@y_zilong ~]#
5、取ifconfig输出结果中的IP地址
[root@y_zilong ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.133 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::7109:ef7e:efd4:722e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:4d:87:05 txqueuelen 1000 (Ethernet)
RX packets 35500 bytes 31847150 (30.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15654 bytes 1444967 (1.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@y_zilong ~]# ifconfig ens33|sed -n '2p'
inet 10.0.0.133 netmask 255.255.255.0 broadcast 10.0.0.255
[root@y_zilong ~]# ifconfig ens33|sed -n '2p'|awk '{print $2}'
10.0.0.133
[root@y_zilong ~]#
[root@y_zilong ~]# ifconfig ens33|awk '/netmask/{print $2}'
10.0.0.133
[root@y_zilong ~]#
6、文件list.txt如下格式,请提取例如 ".baidu.com"前面的主机名部分并写入到回到该文件中
[root@y_zilong ~]# cat list.txt
1 www.baidu.com
2 www.qq.com
3 study.study.com
4 python.python.com
5 yzl.yzl.com
[root@y_zilong ~]# awk -F"[ .]" '{print $2}' list.txt
www
www
study
python
yzl
[root@y_zilong ~]# awk -F"[ .]" '{print $2}' list.txt >> list.txt
[root@y_zilong ~]# cat list.txt
1 www.baidu.com
2 www.qq.com
3 study.study.com
4 python.python.com
5 yzl.yzl.com
www
www
study
python
yzl
[root@y_zilong ~]#