Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep lines from a log file that have the current date?

Tags:

date

grep

logging

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  
like image 443
Jill448 Avatar asked Jun 21 '13 15:06

Jill448


People also ask

How do I grep a log file?

For searching files, the command syntax you use is grep [options] [pattern] [file] , where “pattern” is what you want to search for. For example, to search for the word “error” in the log file, you would enter grep 'error' junglediskserver. log , and all lines that contain”error” will output to the screen.

How do you grep for a specific period in a log?

In the above command, %d/%b/%Y:%H:%M:%S is the format specifier of your date column. You need to update it as per your log file's date format.

How do you use grep to find a line in a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.


2 Answers

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 
like image 135
fedorqui 'SO stop harming' Avatar answered Oct 22 '22 15:10

fedorqui 'SO stop harming'


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
like image 29
John Cooper Avatar answered Oct 22 '22 15:10

John Cooper