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]# 

正是我想要的内容

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值