免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2759 | 回复: 10
打印 上一主题 下一主题

如何查找某个时间段里的日志 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-18 15:25 |只看该作者 |倒序浏览
如题,我想输出/var/log/messages中时间戳在20090311到20090318之间的日志,请问有什么好方法?

论坛徽章:
8
摩羯座
日期:2014-11-26 18:59:452015亚冠之浦和红钻
日期:2015-06-23 19:10:532015亚冠之西悉尼流浪者
日期:2015-08-21 08:40:5815-16赛季CBA联赛之山东
日期:2016-01-31 18:25:0515-16赛季CBA联赛之四川
日期:2016-02-16 16:08:30程序设计版块每日发帖之星
日期:2016-06-29 06:20:002017金鸡报晓
日期:2017-01-10 15:19:5615-16赛季CBA联赛之佛山
日期:2017-02-27 20:41:19
2 [报告]
发表于 2009-03-18 15:31 |只看该作者
sed '/20090311/,/20090318/! d;/20090318/q' /var/log/messages

论坛徽章:
11
金牛座
日期:2015-03-19 16:56:22数据库技术版块每日发帖之星
日期:2016-08-02 06:20:00数据库技术版块每日发帖之星
日期:2016-04-24 06:20:00数据库技术版块每日发帖之星
日期:2016-04-13 06:20:00IT运维版块每日发帖之星
日期:2016-04-13 06:20:00数据库技术版块每日发帖之星
日期:2016-02-03 06:20:00数据库技术版块每日发帖之星
日期:2015-08-06 06:20:00季节之章:春
日期:2015-03-27 15:54:57羊年新春福章
日期:2015-03-27 15:54:37戌狗
日期:2015-03-19 16:56:41数据库技术版块每日发帖之星
日期:2016-08-18 06:20:00
3 [报告]
发表于 2009-03-18 15:37 |只看该作者
sed -n '/2009031[1-8]/p'

论坛徽章:
0
4 [报告]
发表于 2009-03-18 15:38 |只看该作者
原帖由 wg8403 于 2009-3-18 15:25 发表
如题,我想输出/var/log/messages中时间戳在20090311到20090318之间的日志,请问有什么好方法?


awk '/20090311/,/20090318/'  /var/log/messages

论坛徽章:
0
5 [报告]
发表于 2009-03-18 15:40 |只看该作者
sed -n "/20090311/,/20090318/p"  /var/log/messages

论坛徽章:
0
6 [报告]
发表于 2009-03-18 15:52 |只看该作者
非常感谢大家的热情帮助
看来得花点时间学习一下awk或者sed

论坛徽章:
0
7 [报告]
发表于 2009-03-18 15:53 |只看该作者

回复 #5 fly183 的帖子

我看了一下我的服务器的/var/log/messages
时间都是这种,不知道能不能改记录的时间类型。
[root@localhost root]# cat /var/log/messages
Mar 15 04:02:03 localhost syslogd 1.4.1: restart.
Mar 17 15:53:27 localhost sshd(pam_unix)[20706]: session opened for user root by (uid=0)
Mar 17 15:53:44 localhost sshd(pam_unix)[20706]: session closed for user root
Mar 18 15:25:12 localhost sshd(pam_unix)[21276]: session opened for user root by (uid=0)
Mar 18 15:25:38 localhost sshd(pam_unix)[21276]: session closed for user root
Mar 18 15:25:41 localhost sshd(pam_unix)[21329]: session opened for user root by (uid=0)
Mar 18 15:29:46 localhost iptables:  succeeded

论坛徽章:
0
8 [报告]
发表于 2009-03-18 16:13 |只看该作者
原帖由 oldoger 于 2009-3-18 15:53 发表
我看了一下我的服务器的/var/log/messages
时间都是这种,不知道能不能改记录的时间类型。
[root@localhost root]# cat /var/log/messages
Mar 15 04:02:03 localhost syslogd 1.4.1: restart.
Mar 17 15:5 ...



确实不行,请问各位大侠怎么处理这种时间格式的log?

论坛徽章:
0
9 [报告]
发表于 2009-03-18 16:17 |只看该作者

回复 #1 wg8403 的帖子

grep 'Mar [1-8]' /var/log/messages




[w]tail  /var/log/messages
Mar 16 17:50:47 mj-service-tomcat sshd(pam_unix)[9908]: authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=172.18.3.73  user=root
Mar 16 17:51:00 sshd(pam_unix)[9911]: session opened for user root by root(uid=0)
Mar 16 18:00:08 sshd(pam_unix)[10446]: session opened for user root by root(uid=0)
Mar 16 20:58:58 sshd(pam_unix)[21312]: session opened for user root by root(uid=0)
Mar 17 14:49:11 sshd(pam_unix)[23500]: session opened for user root by root(uid=0)
Mar 17 16:15:35 su(pam_unix)[29341]: session opened for user yujx by root(uid=0)
Mar 17 16:17:58 su(pam_unix)[29341]: session closed for user yujx
Mar 17 16:37:51 sshd(pam_unix)[30921]: session opened for user root by root(uid=0)
Mar 18 12:49:54 sshd(pam_unix)[13107]: session opened for user root by root(uid=0)
Mar 18 14:50:18 sshd(pam_unix)[20781]: session opened for user root by root(uid=0)



message里面的时间格式是这样的

论坛徽章:
0
10 [报告]
发表于 2009-03-18 16:18 |只看该作者
改成下面不就可以了?

sed -n "/Mar 11/,/Mar 18/p"  /var/log/messages
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP