摘要:shell + awk


awk

输入分隔符(-F、FS)、输出分隔符(OFS)


awk里引用shell变量

一. awk和shell变量交换的问题

awk和shell变量交换的问题

1、使用 -v选项

2、使用双引号,但是awk内部的$0 11等符号前需要使用\转义

3、使用单引号,将变量引起来,然后前面加一个$符号再次引用变量的值,相当于取2次值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#示例:使用 -v选项
[root@cPen_A ~]# sg="panjinhao"
[root@cPen_A ~]# echo $sg
panjinhao
[root@cPen_A ~]# echo|awk '{print $sg}'

[root@cPen_A ~]# echo|awk -v bsg=$sg '{print bsg}'
panjinhao
[root@cPen_A ~]# ls|awk -v bsg=$sg '{print bsg}'
panjinhao #注:ls 输出多行
panjinhao
……
panjinhao

[root@cPen_A lianxi]# vim test2.sh
awk -v var=$1 -F: '$1==var{print NR,$0}' /etc/passwd
[root@cPen_A lianxi]# bash test2.sh root
1 root:x:0:0:root:/root:/bin/bash
[root@cPen_A lianxi]# bash test2.sh sanchuang
21 sanchuang:x:1000:1000::/home/sanchuang:/bin/bash
#注:第1个$1 位置变量,第2个$1 第1个字段

#示例:使用双引号,但是awk内部的$0 $1等$符号前需要使用\转义
[root@cPen_A lianxi]# mv="zhangjie"
[root@cPen_A lianxi]# useradd zhangjie_123
[root@cPen_A lianxi]# cat /etc/passwd|awk -F: "/^$mv/{print \$1,\$3}"
zhangjie_123 12358

#示例:使用单引号,将变量引起来,然后前面加一个$符号再次引用变量的值,相当于取2次值
[root@cPen_A lianxi]# sg=3
[root@cPen_A lianxi]# awk -F: '/root/{print $1,$'$sg'}' /etc/passwd
root 0
operator 11

二. 进程和进程之间通信的方式

1、共享内存

2、信号量

3、信号

4、管道

5、队列

6、Socket


1
2
3
4
5
6
7
[root@cPen_A lianxi]# cat /etc/shadow
第2个字段:密码字段
*操作系统自带的,没有密码的
!!用户建的,没有密码的
提示:密码字段为*、!!表示没有设置密码;密码字段为空说明密码被清除。
#示例:没有设置密码的用户
[root@cPen_A lianxi]# cat /etc/shadow|awk -F: 'BEGIN{i=0}length($2)<=2{print $1,"没有设置密码";i++}END{print "一共有"i"个用户"}'

三. awk内置函数

awk内置的函数:

​ length()

​ int()

​ sqrt()

​ system()

String Functions

​ sub()

​ index()

​ length()

​ split()

Numeric Functions

yum install python3 安装python3

1
2
3
4
5
6
7
8
#示例:生成0-1之间随机数
[root@cPen_A lianxi]# echo |awk '{print rand()}'
0.237788
#示例:产生0-100之间随机数
[root@cPen_A lianxi]# echo |awk '{print rand()*100}'
23.7788
[root@cPen_A lianxi]# echo |awk '{print int(rand()*100)}'
23

四. 流控

流控:流程控制 control flow

for

while

case

if

1
2
3
4
5
6
7
8
9
10
11
if语句
单分支
[root@cPen_A lianxi]# awk -F: '{if($1 ~ /\<...\>/)print $0}' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
[root@cPen_A lianxi]# awk -F: '$1 ~ /\<.{3}\>/{print $0}' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin

双分支
[root@cPen_A lianxi]# awk -F: '{if($1 ~ /\<...\>/)print $0;else print "不符合要求"}' /etc/passwd
不符合要求
bin:x:1:1:bin:/bin:/sbin/nologin

image-20221011220439733

1
2
3
4
5
6
7
8
9
10
11
12
13
#练习
/etc/passwd
$3是uid 用户的编号
如果用户的编号是0 --> 管理员
1~999 --> 程序用户
大于1000 --> 普通用户
最后统计出有多少管理员,多少程序用户,多少普通用户?
-----------------------------------------------------
[root@cPen_A ~]# awk -F: 'BEGIN{x=0;y=0;z=0}{if($3=="0") {print $1,"管理员";x++} else if($3>=1 && $3<=999) {print $1,"程序用户";y++} else {print $1,"普通用户";z++}}END{print "管理员"x,"程序用户"y,"普通用户"z}' /etc/passwd
root 管理员
bin 程序用户
……
管理员1 程序用户25 普通用户40

五. awk的for循环、数组

image-20221011220532360

image-20221011220538243

image-20221011220551647

image-20221011220605574

image-20221011220614623


awk难点:

  1. 正则表达

  2. if

  3. 内置函数

  4. 内置变量

  5. for,数组array


六. sed

sed是什么?what --> text stream editor
有什么用?where how --> 修改和替换文本 --> modify and substitute

sed是一种支持正则表达式的非交互式流编工具(stream editor)

​ 脚本中修改文本或者文本替换的最佳工具

交互式:一问一答。比如python3里敲命令

image-20221011220732396

2个空间
pattern space --> 处理数据的地方 --> 处理一行数据后,模式空间会清空
hold space --> 临时存放数据的地方

把pattern space放到hold space 命令:h、H

把hold space放到pattern space 命令:g、G

#注:性能意识,sed在内存里运行,比磁盘速度快

#注:容量意识


1
2
3
4
#示例:-i选项  直接在文件里进行修改,不输出到屏幕
[root@cPen_A lianxi]# sed -i 's/xiaomi/redmi/g' test.txt
#示例:通过管道传过来
[root@cPen_A lianxi]# cat chen |sed 's/xiaomi/redmi/g'

image-20221011220845338


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@cPen_A lianxi]# cat -n /etc/passwd|head |tail -6
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
#示例:-n选项 值显示匹配处理的行 (否则会输出所有)
[root@cPen_A lianxi]# cat -n /etc/passwd|sed -n '5,10p'
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

image-20221011220928234

sed的p命令示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
= :输出行号

p命令输出的时候:
连续的行
[root@localhost lianxi]# cat /etc/passwd|sed -n '1,5p'
不连续的行
[root@localhost lianxi]# cat /etc/passwd|sed -n '10p;20p'

示例:sed的p命令示例
sed -n ‘行号1,行号2p’ 输出文件
[root@localhost lianxi]# cat /etc/passwd|sed -n '1,5p' #注:连续用,号
root:x:0:0:root:/root:/bin/bash
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
[root@localhost lianxi]# cat /etc/passwd|sed -n '=;1,5p' #注:=;输出行号
1
root:x:0:0:root:/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
7
……
14
[root@localhost lianxi]# cat -n /etc/passwd|sed -n '1,$p' #注:$p最后一行
[root@localhost lianxi]# cat -n /etc/passwd|sed -n '5,+5p' #注:第5行开始 往后加5行
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@localhost lianxi]# cat -n /etc/passwd|sed -n '5,100!p' #注:取反,取1到4行
1 root:x:0:0:root:/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
#示例:步长值
# cat -n /etc/passwd|sed -n '1~2p'
# cat -n /etc/passwd|sed -n '2~2p'
[root@localhost lianxi]# cat -n /etc/passwd|sed -n '1~2p' #注:单数行,1开始每次往后2行
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11 games:x:12:100:games:/usr/games:/sbin/nologin
13 nobody:x:99:99:Nobody:/:/sbin/nologin
15 dbus:x:81:81:System message bus:/:/sbin/nologin
17 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
19 chrony:x:998:996::/var/lib/chrony:/sbin/nologin
21 chenpeng:x:1001:1001::/home/chenpeng:/bin/bash
23 nginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@localhost lianxi]# cat -n /etc/passwd|sed -n '2~2p' #注:双数行
2 bin:x:1:1:bin:/bin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
8 halt:x:7:0:halt:/sbin:/sbin/halt
10 operator:x:11:0:operator:/root:/sbin/nologin
12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
14 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16 polkitd:x:999:998:User for polkitd:/:/sbin/nologin
18 postfix:x:89:89::/var/spool/postfix:/sbin/nologin
20 mysql:x:1000:1000::/home/mysql:/sbin/nologin
22 redis:x:997:995:Redis Database Server:/var/lib/redis:/sbin/nologin
#示例:shell变量传到sed里
#双引号 推荐
[root@localhost lianxi]# num1=6
[root@localhost lianxi]# num2=10
[root@localhost lianxi]# cat -n /etc/passwd|sed -n "${num1},${num2}p"
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@localhost lianxi]# cat -n /etc/passwd|sed -n ''${num1},${num2}'p'
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

----------------------------------------------------------
#示例:显示包含bash的行
[root@localhost lianxi]# cat /etc/passwd|sed -n '/bash/p'
root:x:0:0:root:/root:/bin/bash
chenpeng:x:1001:1001::/home/chenpeng:/bin/bash
[root@localhost lianxi]# cat /etc/passwd|egrep "bash"
root:x:0:0:root:/root:/bin/bash
chenpeng:x:1001:1001::/home/chenpeng:/bin/bash
[root@localhost lianxi]# cat /etc/passwd|awk '/bash/'
root:x:0:0:root:/root:/bin/bash
chenpeng:x:1001:1001::/home/chenpeng:/bin/bash

#示例:输出#号开头的行
[root@localhost lianxi]# cat /etc/ssh/ssh_config |sed -n '/^#/p'
#示例:输出不是#号开头的行
[root@localhost lianxi]# cat /etc/ssh/ssh_config |sed -n '/^#/!p'
#示例:显示 不是#号和!号开头的行
[root@localhost lianxi]# cat /etc/ssh/ssh_config |sed -r -n '/^#|^$/!p' #注:-r支持更多正则
[root@localhost lianxi]# cat /etc/ssh/ssh_config |egrep -v '^#|^$'
#示例:显示 以/结尾的行
[root@localhost lianxi]# df -h|egrep "/$"
/dev/mapper/centos-root 17G 9.9G 7.2G 58% /
[root@localhost lianxi]# df -h|sed -n '/\/$/p'
/dev/mapper/centos-root 17G 9.9G 7.2G 58% /

#示例:以a-Z开头的行
[root@localhost lianxi]# cat /etc/passwd|sed -n '/^[a-Z]/p'
#示例:不以a-Z开头的行
[root@localhost lianxi]# cat /etc/passwd|sed -n '/^[^a-Z]/p'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
题目:截取nginx的access.log文件里3月18号15:30:00 -- 16:48:10时间段内的所有访问日志

[root@cPen_A lianxi]# service firewalld stop #注:立马停止firewalld服务
[root@cPen_A lianxi]# systemctl disable firewalld #注:设置firewalld服务开机不启动-->下次重启机器的时候不启动

[root@cPen_A nginx]# tail -f access.log #注:动态监控最后一行
192.168.0.17 - - [09/Jan/2021:16:36:03 +0800] "GET /favicon.ico HTTP/1.1" 404 3650 "http://192.168.0.118/" "Mozilla/5.0 (Linux; Android 10; PCT-AL10; HMSCore 5.1.0.300; GMSCore 20.26.14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 HuaweiBrowser/11.0.5.304 Mobile Safari/537.36" "-"
[root@cPen_A nginx]# pwd
/var/log/nginx #注:nginx的日志文件的路径
[root@cPen_A nginx]# ls
access.log access.log-20201202.gz error.log error.log-20201202.gz
#注:access.log是nginx的访问日志文件,记录访问过我们的网站的信息
[root@cPen_A nginx]# cat access.log | awk -F'[()]' '{print $2}'|sort|uniq -c|sort -nr
13 Windows NT 10.0; WOW64 #注:排序去重 再降序排序 显示数量
4 Linux; Android 10; PCT-AL10; HMSCore 5.1.0.300; GMSCore 20.26.14
---------------------------------------------------------------------------
[09/Jan/2021:16:32:46 +0800] --> 09/Jan/2021:16:32:46
[09/Jan/2021:16:33:07 +0800] --> 09/Jan/2021:16:32:46
#示例:查找该时间段的所有访问日志
[root@cPen_A nginx]# cat access.log|sed -n '/09\/Jan\/2021:16:32:46/,/09\/Jan\/2021:16:33:07/p'
#示例:正则写法
[root@cPen_A nginx]# cat access.log|sed -r -n '/09\/Jan\/2021:16:(3[789]|4[0-9]|5[01]):(0[1-9]|[1-5][0-9])/p'
#注:分钟的范围37~51 秒的范围01~59
#37~51
#37~39
#40~49
#50~51

#01~59
0[1-9]|[1-5][0-9]

sed的d命令示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
sed的d命令示例
删除操作可以根据行号和匹配模式进行操作
#示例:删除有denghui的行
[root@cPen_A nginx]# cat /etc/passwd|sed '/denghui/d'
#示例:-i动原文件 删除有redmi这行
[root@cPen_A lianxi]# sed -i '/redmi/d' test.txt
#示例:根据行删除
[root@cPen_A lianxi]# sed -i '2d' test.txt #注:删除第2行
sed查找方式:
1、根据行号
2、根据模式 -->正则表达式=字符+特殊符号
3、根据字符串

#示例:在第4行后面追加huawei
[root@cPen_A lianxi]# sed -i '4a huawei' test.txt
#示例:在第4行前面插入apple
[root@cPen_A lianxi]# sed -i '4i apple' test.txt
#示例:在VIVO这行前一行插入OPPO
[root@cPen_A lianxi]# sed -i '/VIVO/i OPPO' test.txt
#示例:在VIVO同行后面添加APPLE.&表示前面全部
[root@cPen_A lianxi]# sed -i 's/VIVO/& APPLE/' test.txt
#示例:将所有为huawei都替换成sanchuang
[root@cPen_A lianxi]# sed -i 's/huawei/sanchuang/g' test.txt
sed的命令:
p
d
a
i
s
c
r
w