在Linux中使用shell命令从日志中打印两个日期之间的行 [英] Print the lines between two dates from the log using shell command in linux

查看:50
本文介绍了在Linux中使用shell命令从日志中打印两个日期之间的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日志,我想在两个日期之间打印日志中的所有行 02/04/2015:14:23:00 02/04/2015:14:23:59

I have a log and I want to print all the lines from the log between two dates 02/04/2015:14:23:00 and 02/04/2015:14:23:59

推荐答案

使用sed单线的另一种方法: sed -n'/start/,/end/p'log.txt

Another approach with a sed one-liner: sed -n '/start/,/end/p' log.txt

$ cat /tmp/log.txt
before
before
before
a log line containing 02/04/2015:14:23:00 and some other stuff
between
between
a log line containing 02/04/2015:14:23:59 and some other stuff
after
after

$ sed -n '/02\/04\/2015:14:23:00/,/02\/04\/2015:14:23:59/p' /tmp/log.txt
a log line containing 02/04/2015:14:23:00 and some other stuff
between
between
a log line containing 02/04/2015:14:23:59 and some other stuff

请注意,如果开始标记和结束标记在同一行中,它将无法按预期工作.

Please note it will not work as expected if start and end tokens are in the same line.

这篇关于在Linux中使用shell命令从日志中打印两个日期之间的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆