shell按时间段进行日志筛选

这篇博客介绍了如何利用shell命令(sed、grep、awk)来筛选特定时间段的日志,例如15:05到15:10之间的记录。文章通过示例展示了不同命令的用法,并指出了它们在结果上的差异和潜在问题。

筛选15:05--15:10期间的日志

  •  使用sed 

sed -n '/Apr 22 15:05/,/Apr 22 15:10/p' syslog > test

  • 使用grep,与sed提取的行数有差异,待查验。

grep -E '^Apr 22 15:05|^Apr 22 15:06|^Apr 22 15:07|^Apr 22 15:08|^Apr 22 15:09|^Apr 22 15:10'

  • 使用awk,跟grep提取行数一致,不过会因为分隔符原因,时间戳格式被修改,需要额外辨别,待完善。
awk -F'[:]' '$1 = 15 && $2 >=5 && $2 <= 10 {print $0}' syslog 

日志格式:

Apr 22 15:08:28 zrj-debian supervisorctl[4208]:     raise VersionConflict(dist, req).with_context(dependent_req)
Apr 22 15:08:28 zrj-debian supervisorctl[4208]: pkg_resources.VersionConflict: (supervisor 4.1.0 (/usr/local/lib/pytho
n3.5/dist-packages), Requirement.parse('supervisor==3.3.1'))
Apr 22 15:08:28 zrj-debian supervisorctl[4208]: During handling of the above exception, another exception occurred:
Apr 22 15:08:28 zrj-debian supervisorctl[4208]: Traceback (most recent call last):
Apr 22 15:08:28 zrj-debian supervisorctl[4208]:   File "/usr/bin/supervisorctl", line 6, in <module>
Apr 22 15:08:28 zrj-debian supervisorctl[4208]:     from pkg_resources import load_entry_point
Apr 22 15:08:28 zrj-debian supervisorctl[4208]:   File "/usr/local/lib/python3.5/dist-packages/pkg_resources/__init__.
py", line 2927, in <module>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值