如何从具有当前日期的日志文件中 grep 行? [英] How to grep lines from a log file that have the current date?

查看:20
本文介绍了如何从具有当前日期的日志文件中 grep 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用今天的日期 grep 一个日志文件,但输出显示的时间超过了今天的日期.

I need to grep a log file with today's date, but the output is showing for more than today's date.

grep date +"20%y-%m-%d" /path/log/General.log | grep "EmpID#106496" /path/log/Genral.log

输出:

2013-06-19 14:47:05,996 - INFO  EmpID#106496 
2013-06-19 14:47:05,996 - INFO  EmpID#106496 
2013-06-21 00:01:24,915 - INFO  EmpID#106496 
2013-06-21 00:01:24,915 - INFO EmpID#106496  

推荐答案

只需使用 date 输出作为 grep 中的模式:

Just use the date output as a pattern in grep:

$ grep "$(date +"%Y-%m-%d")" file
2013-06-21 00:01:24,915 - INFO   
2013-06-21 00:01:24,915 - INFO

即需要将date语句括起来,才能进行处理.另外,请注意我使用了 Y 而不是你的 20%y.

That is, you need to enclose the date sentence to make it be processed. Also, note I used Y instead of your 20%y.

我正在日志中寻找具有当前日期的特定 EmpID.

I am looking for a sepcific EmpID in the logs with current date.

然后通过管道传输到另一个 grep:

Then pipe to another grep:

$ grep $(date +"%Y-%m-%d") file | grep "EmpID#106496"
2013-06-21 00:01:24,915 - INFO  EmpID#106496 
2013-06-21 00:01:24,915 - INFO EmpID#106496 

这篇关于如何从具有当前日期的日志文件中 grep 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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