Linux三剑客

  • grep:查找过滤
  • sed:编辑
  • awk:分析

这三个命令合称Linux的三剑客,从三剑客这个名字上大家可想而知它们有多厉害,如果把它们的全部内容都讲完的话,至少讲三天三夜,最可怕的是讲了三天三夜,最后你会发现里面很多的知识点是我们搞测试压根用不到的,那个时候你会怀疑人生的,所以在这里为了不让大家怀疑人生,我们就功利一点,只把我们常用的操作掌握了就可以了,学习技术有时候可以功利,但是做人可是不能功利哦 😃。

Linux三剑客之grep

基本使用: 搜索文件中的关键字
grep 关键字 文件路径

grep 常用于查找文件里符合条件的字符串。我们先演示一个基本的操作,这样可以实现一个简单的过滤查找操作。

  1. [root@localhost ~]# cat hello.txt
  2. hello world!
  3. hello world!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. abc
  9. [root@localhost ~]# grep abc hello.txt
  10. abc

练习
搜索 /etc/man_db.conf 文件中 包含 man的内容。

  1. grep man /etc/man_db.conf

image.png
统计 /etc/man_db.conf 文件中 包含 man 总共有多少行。

  1. grep man /etc/man_db.conf | wc

image.png

grep后面跟的这个字符串是可以支持正则表达式的,我们可以把这个调整一下查询hello.txt中以字母a开头的内容,这样写也是可以的

^a 以a开头

  1. [root@localhost ~]# grep ^a hello.txt
  2. abc

查找 /etc/man_db.conf 文件中以MAN开头的内容。

  1. grep ^MAN /etc/man_db.conf

image.png

有时候在查询的时候,我们也忘记需要查询的字符串是大写还是小写了,这个时候可以使用忽略大小写功能

-i 忽略大小写

  1. [root@localhost ~]# grep ABC hello.txt
  2. [root@localhost ~]# grep -i ABC hello.txt
  3. abc

统计 /etc/man_db.conf 文件 以man开头的内容 (不区分大小写)。
image.png

还有一些场景需要查询出来对应字符串所在的行号,方便我们快速在文件中定位字符串所在的位置,这个也很简单,通过-n参数就可以实现。


-n 显示行号

  1. [root@localhost ~]# grep -i ABC -n hello.txt
  2. 7:abc

显示行号,并忽略大小写,从/etc/man_db.conf 文件中搜索 man

  1. grep -in ^man /etc/man_db.conf

image.png
最后再教大家一招,假设我们想要查看一下服务器中运行的java进程,那我们可以使用前面讲的ps命令加上grep命令来实现ps -ef | grep java显示出这个信息其实说明没有找到java进程信息,下面返回的这一行表示是grep进程本身,这样容易给我们造成错觉,想把它去掉,怎么去掉呢?

  1. [root@localhost ~]# ps -ef | grep java
  2. root 2497 2247 0 22:34 pts/1 00:00:00 grep --color=auto java

很简单,使用grep加上-v参数再做一次过滤即可,表示忽略包含指定字符串的数据。

-v 屏蔽指定的字符串

  1. [root@localhost ~]# ps -ef | grep java | grep -v grep

显示行号,并忽略大小写,从/etc/man_db.conf 文件中搜索 man,如果搜索结果中包含 share,请将这一行屏蔽掉。

  1. grep -in ^man /etc/man_db.conf | grep -v share

image.png

$ 指定字符结尾

查找 /etc/man_db.conf 文件,找到以 man结尾 的内容。

  1. grep -n man$ /etc/man_db.conf

image.png

ok 这些就是针对grep最常见的一些用法,前期大家掌握这些就足够使用了!

更多关于 grep 的使用。

Linux三剑客之sed

接下来我们看一下Linux的第二个剑客,sed之前我们修改文件是使用vi命令,大家可以想象一个场景,如果让你同时去修改上千个文件里面的某一行内容,你再使用vi去操作,是不是有点手软,虽然这个上千个文件有点夸张,但是在实际工作中类似这样同时修改10几个文件的场景还是有的,大家不要想着,就10几个文件,我使用vi命令不到10分钟就搞完了,如果你这样想,那就完了,程序员一定要懒,这个懒是指能用程序去做的,千万不要动手。假设你花费5分钟写一个自动化程序,然后可以在秒级别内解决掉这十几个文件,这样是不是还剩下5分钟时间。

首先看一下,如何通过sed命令向文件中添加一行内容,我的需求是这样的,我想要在hello.txt文件的第二行内容下面添加一行内容

  • a 在指定位置之后添加 2a 在第二行之后添加
  • i 在指定位置之前添加 2i 在第二行之前添加
  1. [root@localhost ~]# cat hello.txt
  2. hello world!
  3. hello world!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. abc
  9. [root@localhost ~]# sed '2a\haha' hello.txt
  10. hello world!
  11. hello world!
  12. haha
  13. hello world!
  14. hello world!
  15. hello world!
  16. hello world!
  17. abc

但是你回头再来查看这个文件的内容发现数据并没有被真正添加进去,注意了,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据,最终把缓冲区内的数据输出到控制台。

那能不能直接修改源文件数据呢? 当然可以了,具体的操作方法我们一会再说,现在这种方式我们测试起来非常方便,不会影响源文件。

刚才的a参数表示向指定行的下面添加数据,但是如果我们想要在第一行添加数据怎么办呢?sed ‘1a\haha’ hello.txt 此操作会将数据添加到第一行下面(也就是第二行的位置)sed ‘0a\haha’ hello.txt 此操作会报错,行号是从1开始的。

  1. [root@localhost ~]# sed '1a\haha' hello.txt
  2. hello world!
  3. haha
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. hello world!
  9. abc
  10. [root@localhost ~]# sed '0a\haha' hello.txt
  11. sed: -e expression #1, char 2: invalid usage of line address 0

那这个时候怎么办呢? 不要着急,还有一个参数 1i的意思表示在指定行的前面插入一行,它的使用方式和参数a一样我们来演示一下

  1. [root@localhost ~]# sed '1i\haha' hello.txt
  2. haha
  3. hello world!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. hello world!
  9. abc

所以说这个a其实表示是append的意思,在指定行后面追加内容i表示是insert的意思,是在指定行的前面插入内容好 ,这是a和i的区别。

不知道大家有没有思考一个问题,如果我们想要在一个文件的最后一行的前面添加一行内容,这个时候怎么办呢?我们可以先去看一下文件的行数,然后再过来操作,这样当然是没有问题的,就是比较麻烦,那能不能快速定位到最后一行呢?可以的,在这里我们可以通过一个特殊参数 $ 它在这里表示是最后一行的意思

$ 表示最后一行

  1. [root@localhost ~]# sed '$i\haha' hello.txt
  2. hello world!
  3. hello world!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. haha
  9. abc

ok,这就是增加数据的操作

下面我们来看一下删除操作,有时候我们想要删除文件中多余的行,这个时候只需要使用参数d就可以搞定假设我们想删除文件中的第7行内容

d (delete) 删除

  1. [root@localhost ~]# sed '7d' hello.txt
  2. hello world!
  3. hello world!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!

当然了这里也可以使用$参数,$d删除最后一行,更加便捷

  1. [root@localhost ~]# sed '$d' hello.txt
  2. hello world!
  3. hello world!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!

删除倒数第3行的内容。

  1. tac hello1.txt | sed '3d' | tac

image.png
删除3,4,5行内容。

  1. sed '3,5d' hello.txt

image.png

  • 3,5d 表示从第3行到第5行。

修改文件内容

ok,这是删除数据的操作接下来我们来看一下替换操作,这个也是比较常见的,最常见的场景就是修改配置文件里面的服务器地址相关信息,以及账号密码什么的。

下面我们来操作修改一下文件内容中的l这个字符sed ‘s/l/a/1’ hello.txt

sed后面的参数格式为[address]s/pattern/replacement/flags这里的address 表示指定要操作的具体行,是一个可选项s 表示替换操作,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容,flags有多种用法,我们挑两种说一下第一种就是flags可以表示为1~512之间的任意一个数字,表示指定要替换的字符串在这一行中出现第几次时才进行替换第二种就是flags可以直接表示为g,这样的意思就是对每一行数据中所有匹配到的内容全部进行替换如果flags位置的值为空,则只会在第一次匹配成功时做替换操作

  • s/l/a/1 s 表示搜索 l ,将l替换为a, 最后的 1 表示每行的第一个
  • s/l/a/2 s 表示搜索 l ,将l替换为a, 最后的 2 表示每行的第2个
  1. [root@localhost ~]# sed 's/l/a/1' hello.txt
  2. healo world!
  3. healo world!
  4. healo world!
  5. healo world!
  6. healo world!
  7. healo world!
  8. abc
  9. [root@localhost ~]# sed 's/l/a/2' hello.txt
  10. helao world!
  11. helao world!
  12. helao world!
  13. helao world!
  14. helao world!
  15. helao world!
  16. abc
  17. [root@localhost ~]# sed 's/l/a/3' hello.txt
  18. hello worad!
  19. hello worad!
  20. hello worad!
  21. hello worad!
  22. hello worad!
  23. hello worad!
  24. abc
  25. [root@localhost ~]# sed 's/l/a/g' hello.txt
  26. heaao worad!
  27. heaao worad!
  28. heaao worad!
  29. heaao worad!
  30. heaao worad!
  31. heaao worad!
  32. abc
  33. [root@localhost ~]# sed 's/l/a/' hello.txt
  34. healo world!
  35. healo world!
  36. healo world!
  37. healo world!
  38. healo world!
  39. healo world!
  40. abc

替换hello.txt 文件中每1行中的前2个 l 为 a。

我们现在的替换操作都是会匹配文件中的所有行,如果我们只想替换指定行中的内容怎么办呢?只需要增加address 参数即可。

  • 2s/l/a/g 2s 只替换第2行的内容。
  • 2,5s/l/a/g 2,5s 从第2行开始 到第5行结束。
  1. [root@localhost ~]# sed '2s/l/a/g' hello.txt
  2. hello world!
  3. heaao worad!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. abc
  9. [root@VM-0-14-centos tmp]# sed '2,5s/l/a/g' hello.txt
  10. hello world!
  11. heaao worad!
  12. heaao worad!
  13. heaao worad!
  14. heaao worad!
  15. hello world!
  16. abc

这就是替换操作的常见应用场景

注意了,咱们前面所讲的sed命令的所有操作,在执行之后都不会修改源文件中的内容,这样只能作为测试,如果需要修改源文件的话,其实也很简单,只需要增加一个 -i 参数即可,我们来演示一下

-i 修改源文件

  1. [root@localhost ~]# sed -i '2s/l/a/g' hello.txt
  2. [root@localhost ~]# cat hello.txt
  3. hello world!
  4. heaao worad!
  5. hello world!
  6. hello world!
  7. hello world!
  8. hello world!
  9. abc

ok,针对sed命令我们就讲到这。

Linux三剑客之awk

最后我们来看一下第三个剑客 awk,awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大,简单来说awk就是把文件逐行的读入,以空白字符为默认分隔符将每行内容切片,切开的部分再进行各种分析处理。

awk的基本格式:awk [option] programe file这里的option是一个可选项,一般在这里来指定文件中数据的字段分隔符programe 是具体的处理逻辑file表示我们要操作的文件

在具体使用awk之前我们还需要掌握awk的一个特性,就是awk在处理文本数据的时候,它会自动给每行中的每个字段分配一个变量变量从1开始,$1表示是文本中的第1个数据字段$2表示是文本中的第2个数据字段以此类推。还有一个特殊的 $0 它代表整个文本行的内容

那下面我们来操作一下hello.txt这个文件这个文件的字段中间默认是空格

  1. [root@localhost ~]# cat hello.txt
  2. hello world!
  3. heaao worad!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. abc
  9. [root@localhost ~]# awk '{print $1}' hello.txt
  10. hello
  11. heaao
  12. hello
  13. hello
  14. hello
  15. hello
  16. abc
  17. [root@localhost ~]# awk '{print $2}' hello.txt
  18. world!
  19. worad!
  20. world!
  21. world!
  22. world!
  23. world!
  24. [root@localhost ~]# awk '{print $0}' hello.txt
  25. hello world!
  26. heaao worad!
  27. hello world!
  28. hello world!
  29. hello world!
  30. hello world!
  31. abc

将 acccess.log 文件上传到 /tmp 目录下。
access.log
输出 /tmp/access.log 文件中的 所有ip地址。 ip地址位于每一行的第一列。

  1. awk '{print $1}' /tmp/access.log

image.png

统计文件中的ip 地址,去重后统计有多少个ip地址。

  1. awk '{print $1}' /tmp/access.log | sort | uniq | wc

image.png
linux中还有一个文件 /etc/passwd 里面存储的是用户信息但是这个文件中的字段之间是使用:分割的,这个时候想要操作某列字段的话就需要我们手工指定字段分隔符了。

咱们前面说了,可以在option里面指定字段分隔符,通过-F 参数awk -F: ‘{print $1}’ /etc/passwd

-F 指定分割符

  1. [root@localhost ~]# awk -F: '{print $1}' /etc/passwd
  2. root
  3. bin
  4. daemon
  5. adm
  6. lp
  7. sync
  8. shutdown
  9. halt
  10. mail
  11. operator
  12. games
  13. ftp
  14. nobody
  15. systemd-network
  16. dbus
  17. polkitd
  18. sshd
  19. postfix

处理指定包含内容

我们在处理数据的时候还可以对数据进行一些过滤操作,只获取满足条件的数据在programe中可以使用正则表达式进行过滤,awk ‘/world/ {print $1}’ hello.txt 这种写法表示对每次读取到的那一行数据进行匹配

  1. [root@localhost ~]# cat hello.txt
  2. hello world!
  3. heaao worad!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. abc
  9. [root@localhost ~]# awk '/world/ {print $0}' hello.txt
  10. hello world!
  11. hello world!
  12. hello world!
  13. hello world!
  14. hello world!
  15. [root@localhost ~]# awk '/abc/ {print $0}' hello.txt
  16. abc

使用awk 命令 查看 /etc/man_db.conf 文件中 包含 man的行中的内容。

  1. awk '/man/ {print $0}' /etc/man_db.conf

image.png

指定行

如果我们只想对某一列数据进行匹配呢?awk ‘($1 ~ /world/) {print $1}’ hello.txt 在这里面可以通过$来指定具体是哪一列,需要把具体的对比逻辑放到小括号里面

  • ($1 ~ /world/) 指定第一列,从第一列中搜索 world
  • ($2 ~ /world/) 指定第二列,从第二列中搜索 world
  1. [root@localhost ~]# awk '($1 ~ /world/) {print $0}' hello.txt
  2. [root@localhost ~]# awk '($2 ~ /world/) {print $0}' hello.txt
  3. hello world!
  4. hello world!
  5. hello world!
  6. hello world!
  7. hello world!
  8. [root@localhost ~]# awk '($2 ~ /wor[a-z]d/) {print $0}' hello.txt
  9. hello world!
  10. heaao worad!
  11. hello world!
  12. hello world!
  13. hello world!
  14. hello world!

注意:这里如果是不匹配的意思的话需要使用 !~

学习任务

针对apache中的access访问日志进行分析
access.log

  1. 查看所有的ip 地址,

    1. awk '{print $1}' /tmp/access.log
  2. 所有的ip地址 去重 并排序

  1. [root@VM-0-14-centos logs]# awk '{print $1}' access.log | sort | uniq
  2. 106.53.123.231
  3. 106.75.211.240
  4. 106.75.251.234
  5. 112.64.119.204
  6. 112.65.61.166
  7. 114.132.240.211
  8. 114.85.158.77
  9. 115.205.144.130
  10. 116.235.32.47
  11. 116.235.32.50
  12. 117.136.120.250
  13. 117.136.8.206
  14. 119.90.42.91
  15. 119.90.52.38
  16. 122.234.89.243
  17. 125.25.17.235
  18. 128.14.209.162
  19. 138.68.19.237
  20. 140.207.23.108
  21. 140.207.23.255
  22. 143.198.136.88
  23. 143.244.183.157
  24. 147.182.226.61
  25. 147.182.226.83
  26. 157.55.39.73
  27. 164.92.246.22
  28. 165.232.161.227
  29. 165.232.173.178
  30. 165.232.175.155
  31. 165.232.175.77
  32. 175.107.1.64
  33. 177.143.255.152
  34. 180.163.220.4
  35. 180.163.220.5
  36. 180.163.220.66
  37. 180.163.220.67
  1. 统计每个ip地址出现的次数。 比如 180.163.220.4 出现2次。 控制台输出
    1. 2 180.163.220.4

  1. awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -10

image.png

tail -f 实时追踪日志

  1. 启动禅道服务

    1. /opt/zbox/zbox start
  2. 实时查看禅道的访问日志 ```bash tail -f /opt/zbox/logs/apache_access.log

  1. ---
  2. <a name="wN1Ln"></a>
  3. # 作业
  4. 1. 服务器上启动 禅道服务
  5. 1. 使用netstat 命令查看已经启动的端口。需要找到 80端口 3306端口 说明禅道启动成功
  6. 1. 使用 kill 命令结束 80端口对应的进程 kill pid
  7. 1. 使用 killall 命令结束 3306 端口对应的进程 需要进程名
  8. 1. 使用 ps 命令检查 zbox 相关的进程信息
  9. 1. 查看cpu,内存信息
  10. 1. 单独查看内存信息
  11. 1. 查看磁盘相关信息
  12. 1. 在/root 目录下创建 fanmao/85/01/ 文件夹, fanmao/85/02/文件夹
  13. 1. /root/fanmao/85/01/ 目录下创建 fanmao.1G 文件,文件的大小为1G
  14. 1. 在/root/fanmao/85/02/ 目录下创建 fanmao.2G 文件, 文件的大小为2G
  15. 1. /root 目录下 找到 文件大小在 1G - 2G 之间(包含1G 2G)的文件
  16. ---
  17. 1. 重启启动 禅道服务
  18. 1. 浏览器上 登录禅道, 编写一条测试用例, 测试用例为 【外卖下订单流程】,要求至少包含用户操作,商家操作,骑手操作。(至少10个操作步骤)
  19. 1. 查找文件路径 文件名中包含 access 并且文件名的后缀名为 .log
  20. ```bash
  21. find / -name '*access*.log'
  1. locate access | grep '.log$'
  1. 统计 /opt/zbox/logs/apache_access.log 文件中的所有ip 并按照ip出现的次数 从大到小 输出出来。

    1. awk '{print $1}' /opt/zbox/logs/apache_access.log | sort | uniq -c | sort -rn
  2. 将 /opt/zbox/logs/apache_access.log 文件中 ip地址是 以 116 开头(比如 116.227.31.240)的 这一行内容找到 并 重定向到 /tmp/ip.txt 文件中。

    1. grep '^116' /opt/zbox/logs/apache_access.log > /tmp/ip.txt
    1. awk '($1 ~ /^116/) {print $0}' /opt/zbox/logs/apache_access.log > /tmp/ip.txt

怎么查看昨天的日志 加分
date 命令可以查看日期
access.log
找出 系统中最大的 5个文件。