Shell截取文本一段区间的方法

假设有一段日志,我需要截取 2019/09/232019/09/29 之间期间的日志。
那么,我利用shell怎么去截取呢
在这里插入图片描述

2019/09/16 09:56:00 [warn] 11#11: could not build optimal proxy_hea
2019/09/16 09:56:00 [warn] 11#11: could not build optimal proxy_hea
2019/09/16 09:56:00 [warn] 11#11: could not build optimal proxy_hea
2019/09/16 09:56:00 [warn] 11#11: could not build optimal proxy_hea
2019/09/23 04:47:27 [crit] 12#12: *5741 SSL_do_handshake() failed (
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/29 20:02:05 [crit] 11#11: *3083 SSL_do_handshake() failed (
2019/09/30 17:36:46 [warn] 10#10: could not build optimal proxy_hea
2019/09/30 17:36:46 [warn] 10#10: could not build optimal proxy_hea
2019/09/30 17:36:46 [warn] 10#10: could not build optimal proxy_hea

实现方法

先利用sed -n截取2019/09/232019/09/29 关键词

sed -n '/^2019\/09\/23/,/2019\/09\/29/p' log.txt

这里是利用sed -n 截取2019/09/232019/09/29 区间的日志
因为有/字符,需要在 /前面加上\转义一下。就变成了\/,有些特殊字符在shell中都需要转义,否则会与偶问题。具体有哪些字符需要转义,可以百度shell转义字符

sed -n '/^a/,/bp/'  file

这个就是截取a和b之间的字符,包括a和b

为什么在a前面加上一个^这个是一个正则,表示从a开头开始匹配

以上的结果为:

[root@aliyun test]# sed -n '/^2019\/09\/23/,/2019\/09\/29/p' log.txt
2019/09/23 04:47:27 [crit] 12#12: *5741 SSL_do_handshake() failed (
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/29 20:02:05 [crit] 11#11: *3083 SSL_do_handshake() failed (
[root@aliyun test]# 

在这里插入图片描述

接下来,可以过滤 2019/09/232019/09/29

sed -n '/^2019\/09\/23/,/2019\/09\/29/p' log.txt | grep -Ev '(^2019\/09\/23 |2019\/09\/29)'

用grep过滤即可 grep -E 表示过滤带有正则表达式的内容 -v 表示不显示

结果为:

[root@aliyun test]# sed -n '/^2019\/09\/23/,/2019\/09\/29/p' log.txt | grep -Ev '(^2019\/09\/23 |2019\/09\/29)'
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
2019/09/25 12:41:35 [warn] 10#10: could not build optimal proxy_hea
[root@aliyun test]# 

正是我想要的内容

在这里插入图片描述

发布了147 篇原创文章 · 获赞 72 · 访问量 49万+

猜你喜欢

转载自blog.csdn.net/diyiday/article/details/102758007