Shell-day14
1. 昨日回顾
1. Shell正则表达式
2. 什么是正则表达式
3. 常见的通配符和特殊符号
4. 正则表达式的符号介绍 基础正则 扩展正则
5. 正则表达式之Grep应用
6. 正则表达式之Sed应用 工作原理
2. Awk基本介绍
awk是一种编程语言 gawk 对于底层的操作系统的文本及数据处理
awk处理数据的方式:
1. 读入文件,按行进行读取,从第一行到最后一行
2. 寻找匹配的条件的行,在行上进行操作mysql主从同步及读写分离配置
3. 如果此行没有满足条件的,会丢弃,没有指定条件的,会把当前行内容全部打印出来
awk的语法
awk [option] 'command' files
行处理前 行处理中 行处理后
BEGIN{} {} END{}
[root@qls ~]
2
localhost.localdomain
localhost.localdomain
命令结束
[root@qls ~]
2
[root@qls ~]
1
[root@qls ~]
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
localhost.localdomain
localhost.localdomain
[root@qls ~]
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@qls ~]
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@qls ~]
5050
[root@qls ~]
5050
3. Awk的工作原理
[root@qls ~]
1. awk将文件中的每一行作为输入,并将每一行处理的时候放入到特定的模式空间中,将每一行的数据赋值给内部变量$0, 以换行符结束
2. awk开始进行字段分解,每个字段存储在自己的内部变量中, 变量从$1开始
3. awk默认字段分割是由内部变量FS来指定的,可以使用-F修订,默认的分割符为空白字符
4. awk行处理时使用print来打印分割后的字段
5. awk在打印之后会给不同的字段之间加上空格作为输出分隔符,这个分割符是由书写的逗号进行映射,逗号被映射到内部变量输出分隔符OFS,默认输出分隔符就是空格
6. awk输出之后,将从文件中读取下一行内容,并将其存储到$0变量中,覆盖原来的内容,然后接着进行循环处理,直到文件行处理结束
4. Awk命令格式
匹配的模式
[root@qls ~]
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@qls ~]
root
operator
[root@qls ~]
47392452
5. Awk内部变量
[root@qls ~]
127.0.0.1
::1
[root@qls ~]
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@qls ~]
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@qls ~]
10.0.0.81
[root@qls ~]
10.0.0.81
输出分隔符默认为空格 使用逗号进行映射
[root@qls ~]
root@x
bin@x
daemon@x
adm@x
lp@x
sync@x
shutdown@x
halt@x
mail@x
operator@x
[root@qls ~]
root$x
bin$x
daemon$x
adm$x
lp$x
sync$x
shutdown$x
halt$x
mail$x
operator$x
[root@qls ~]
root$x
bin$x
daemon$x
adm$x
lp$x
sync$x
shutdown$x
halt$x
mail$x
operator$x
[root@qls ~]
7 /bin/bash
7 /sbin/nologin
7 /sbin/nologin
7 /sbin/nologin
7 /sbin/nologin
7 /bin/sync
7 /sbin/shutdown
7 /sbin/halt
7 /sbin/nologin
7 /sbin/nologin
[root@qls ~]
6 /bin/bash
7 /sbin/nologin
7 /sbin/nologin
7 /sbin/nologin
7 /sbin/nologin
7 /bin/sync
7 /sbin/shutdown
7 /sbin/halt
7 /sbin/nologin
7 /sbin/nologin
[root@qls ~]
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
/sbin/nologin
[root@qls ~]
root
/bin
/sbin
/var/adm
/var/spool/lpd
/sbin
/sbin
/sbin
/var/spool/mail
/root
[root@qls ~]
1 root:x:0:0:root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
root
[root@qls ~]
1 root:x:0:0:root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
11 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
12 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@qls ~]
1 root:x:0:0:root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 operator:x:11:0:operator:/root:/sbin/nologin
1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
2 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@qls ~]
今年是:2020 月份为:May
[root@qls ~]
User: root UID: 0 GID: 0
User: bin UID: 1 GID: 1
User: daemon UID: 2 GID: 2
User: adm UID: 3 GID: 4
User: lp UID: 4 GID: 7
User: sync UID: 5 GID: 0
User: shutdown UID: 6 GID: 0
User: halt UID: 7 GID: 0
User: mail UID: 8 GID: 12
User: operator UID: 11 GID: 0
[root@qls ~]
User: root UID: 0 GID: 0
User: bin UID: 1 GID: 1
User: daemon UID: 2 GID: 2
User: adm UID: 3 GID: 4
User: lp UID: 4 GID: 7
User: sync UID: 5 GID: 0
User: shutdown UID: 6 GID: 0
User: halt UID: 7 GID: 0
User: mail UID: 8 GID: 12
User: operator UID: 11 GID:
awk -F: '{printf "%-18s %-10s %-10s","User: "$1,"UID: "$3,"GID: "$4}' passwd
User: root UID: 0 GID: 0 User: bin UID: 1 GID: 1 User: daemon UID: 2 GID: 2 User: adm UID: 3 GID: 4 User: lp UID: 4 GID: 7 User: sync UID: 5 GID: 0 User: shutdown UID: 6 GID: 0 User: halt UID: 7 GID: 0 User: mail UID: 8 GID: 12 User: operator UID: 11 GID: 0 [root@shell /service/scripts/day14]
[root@qls ~]
User: root UID: 0 GID: 0
User: bin UID: 1 GID: 1
User: daemon UID: 2 GID: 2
User: adm UID: 3 GID: 4
User: lp UID: 4 GID: 7
User: sync UID: 5 GID: 0
User: shutdown UID: 6 GID: 0
User: halt UID: 7 GID: 0
User: mail UID: 8 GID: 12
User: operator UID: 11 GID: 0
6. Awk模式动作
1. 正则表达式
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
halt:x:7:0:halt:/sbin:/sbin/halt
[root@qls ~]
root:x:0:0:root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
halt:x:7:0:halt:/sbin:/sbin/halt
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
2. 比较表达式
两个数值或者字符进行比较,只有条件为真时,才会执行指定的动作
关系运算符
<
>
<=
>=
==
!=
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
root
[root@qls ~]
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
[root@qls ~]
halt 7
mail 8
operator 11
[root@qls ~]
shutdown 6
halt 7
mail 8
operator 11
[root@qls ~]
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
shutdown 6
[root@qls ~]
root 0
bin 1
daemon 2
adm 3
lp 4
sync 5
halt 7
mail 8
operator 11
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
root:x:0:0:root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@qls ~]
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
bin:x:1:1:bin:/bin:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
bin:x:1:1:bin:/bin:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
root:x:0:0:root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
[root@qls ~]
/dev/sda3 50101352 2709024 47392328 6% /
[root@qls ~]
磁盘根分区空间使用过多
[root@qls ~]
[root@qls ~]
[root@qls ~]
管理员root
普通用户bin
普通用户daemon
普通用户adm
普通用户lp
普通用户sync
普通用户shutdown
普通用户halt
普通用户mail
普通用户operator
[root@qls ~]
管理员root
[root@qls ~]
管理员root
[root@qls ~]
2
[root@qls ~]
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
8
[root@qls ~]
80
[root@qls ~]
18
[root@qls ~]
-2
[root@qls ~]
1073741824
&& || !
[root@qls ~]
管理员为:root
[root@qls ~]
root
bin
[root@qls ~]
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
root:x:0:0:root:/bin/bash
[root@qls ~]
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
root:x:0:0:root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@qls ~]
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
7. Awk的条件判断
if 表达式 动作
if 表达式 动作 else 动作
if 表达式 动作 else if 表达式 动作 else 动作
[root@qls ~]
root为系统管理员用户
bin为系统管理员用户
[root@qls ~]
系统用户的数量为:49
[root@qls ~]
系统用户的数量为:26
[root@qls ~]
普通用户的数量为:23
[root@qls ~]
普通用户的数量为:22
[root@qls ~]
系统用户的数量为:27
[root@qls ~]
管理员数量为:1 系统用户数量为:49
[root@qls ~]
管理员数量为:1 系统用户数量为:26 普通用户数量为:23
8. Awk的循环语句
[root@qls ~]
1
2
3
4
5
6
7
8
9
10
[root@qls ~]
[root@qls ~]
[root@qls ~]
1
2
3
4
5
6
7
8
9
10
a=1
while [ a -le 10 ]
do
echo a
let a++
done
[root@qls ~]
[root@qls ~]
9. Awk数组
i++
i+=
awk shell关联数组
赋值: Shells[$NF]++ == let Shells[$NF]++
打印: for (i in Shells) == for i in ${!Shells[@]}
Shells == ${!Shells[@]}
索引值 Shells[i] == ${Shells[$i]}
[root@qls ~]
Shell: /bin/sync Count: 1
Shell: /bin/bash Count: 23
Shell: /sbin/nologin Count: 24
Shell: /sbin/halt Count: 1
Shell: /sbin/shutdown Count: 1
[root@qls ~]
State: LISTEN Count: 7
State: ESTABLISHED Count: 1
[root@qls ~]
5050
[root@qls ~]
101.4.60.63 - - [30/Sep/2019:03:11:52 +0800] "HEAD /wp-content/themes/QQ2.8/js/mousewheel.js?ver=1.0.0 HTTP/1.1" 401 0 "-" "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Mobile Safari/537.36" "-"
[root@qls ~]
37590 access.log
[root@qls ~]
37590
[root@qls ~]
37590
[root@qls ~]
IP地址为: 139.226.172.217 访问次数为: 16033
IP地址为: 47.102.42.79 访问次数为: 1041
IP地址为: 180.95.238.82 访问次数为: 993
IP地址为: 113.140.249.33 访问次数为: 726
IP地址为: 223.166.74.124 访问次数为: 646
IP地址为: 112.87.125.55 访问次数为: 525
IP地址为: 182.138.163.167 访问次数为: 429
IP地址为: 112.87.125.53 访问次数为: 373
IP地址为: 223.166.74.198 访问次数为: 352
IP地址为: 182.138.158.169 访问次数为: 308
[root@qls ~]
访问次数大于100次的IP地址为: 139.226.172.217 访问次数为: 16033
访问次数大于100次的IP地址为: 47.102.42.79 访问次数为: 1041
访问次数大于100次的IP地址为: 180.95.238.82 访问次数为: 993
访问次数大于100次的IP地址为: 113.140.249.33 访问次数为: 726
访问次数大于100次的IP地址为: 223.166.74.124 访问次数为: 646
访问次数大于100次的IP地址为: 112.87.125.55 访问次数为: 525
访问次数大于100次的IP地址为: 182.138.163.167 访问次数为: 429
访问次数大于100次的IP地址为: 112.87.125.53 访问次数为: 373
访问次数大于100次的IP地址为: 223.166.74.198 访问次数为: 352
访问次数大于100次的IP地址为: 182.138.158.169 访问次数为: 308
访问次数大于100次的IP地址为: 117.136.8.136 访问次数为: 260
访问次数大于100次的IP地址为: 113.140.251.30 访问次数为: 245
访问次数大于100次的IP地址为: 116.235.209.131 访问次数为: 219
访问次数大于100次的IP地址为: 203.34.152.133 访问次数为: 201
访问次数大于100次的IP地址为: 183.192.9.208 访问次数为: 162
访问次数大于100次的IP地址为: 222.68.114.200 访问次数为: 160
访问次数大于100次的IP地址为: 101.86.185.116 访问次数为: 153
访问次数大于100次的IP地址为: 61.171.179.116 访问次数为: 140
访问次数大于100次的IP地址为: 223.64.230.30 访问次数为: 140
访问次数大于100次的IP地址为: 139.227.12.139 访问次数为: 132
访问次数大于100次的IP地址为: 220.112.121.208 访问次数为: 127
访问次数大于100次的IP地址为: 123.126.113.152 访问次数为: 112
[root@qls ~]
139.226.172.217 16033
47.102.42.79 1041
180.95.238.82 993
113.140.249.33 726
223.166.74.124 646
112.87.125.55 525
182.138.163.167 429
112.87.125.53 373
223.166.74.198 352
182.138.158.169 308
117.136.8.136 260
113.140.251.30 245
116.235.209.131 219
203.34.152.133 201
183.192.9.208 162
222.68.114.200 160
101.86.185.116 153
61.171.179.116 140
223.64.230.30 140
139.227.12.139 132
220.112.121.208 127
123.126.113.152 112
awk '{Url[$11]++}END{for(i in Url){print "访问的Url:"i,"Count:"Url[i]}}' access.log |sort -rnk4
awk '{State[$9]++}END{for(i in State){print "状态码:"i,"Count:"State[i]}}' access.log
每个ip访问的总大小:awk '{Ip[$1]++;Sum[$1]+=$10}END{for(i in Ip){print "IP:"i,"Sum:"Sum[i]}}' access.log
所有访问的总大小:awk '{Sum+=$10}END{print Sum}' access.log
[root@qls ~]
IP: 139.226.172.217 State: 200 Count: 14731
IP: 139.226.172.217 State: 206 Count: 88
IP: 139.226.172.217 State: 404 Count: 193
IP: 139.226.172.217 State: 499 Count: 99
IP: 139.226.172.217 State: 302 Count: 108
IP: 139.226.172.217 State: 304 Count: 805
IP: 139.226.172.217 State: 504 Count: 9
[root@qls ~]
IP: 139.226.172.217 Count: 16033 Size: 749869585
IP: 47.102.42.79 Count: 1041 Size: 16462
IP: 180.95.238.82 Count: 993 Size: 154136
IP: 113.140.249.33 Count: 726 Size: 105270
IP: 223.166.74.124 Count: 646 Size: 93670
IP: 112.87.125.55 Count: 525 Size: 15640394
IP: 182.138.163.167 Count: 429 Size: 62205
IP: 112.87.125.53 Count: 373 Size: 10792077
IP: 223.166.74.198 Count: 352 Size: 61217
IP: 182.138.158.169 Count: 308 Size: 44660
awk '{Ip_Url["ip:"$1 "url:"$11]++;Sum["ip:"$1 "url:"$11]+=$10}END{for (i in Ip_Url){print "IP: "i,"Count: "Ip_Url[i],"size: "Sum[i]}}' access.log
awk '{State[$9]++}END{print "State: "200,"Count: "State[200]}' access.log
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现