Grep a log file with current date

2019-08-08 17:22发布

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

Output:

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  

3条回答
The star\"
2楼-- · 2019-08-08 18:09

If you need to add space delimited fields in date command use double quotes around $() :

$ grep "$(date +"%Y-%m-%d %H:%M")" file
2013-06-21 00:01:24,915 - INFO   
2013-06-21 00:01:24,915 - INFO
查看更多
Summer. ? 凉城
3楼-- · 2019-08-08 18:14

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

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


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

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 
查看更多
劳资没心,怎么记你
4楼-- · 2019-08-08 18:19

You're not actually executing 'date', because it's not been surrounded by backticks. The command should be

grep `date +"20%y-%m-%d"` /path/log/General.log
#    ^--                ^--

Right now, as written, you are searching for the word date in two different files (+"20%Y-%m-%d", and general.log).

查看更多
登录 后发表回答