LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-08-2013, 07:23 PM   #1
JJJCR
Member
 
Registered: Apr 2010
Posts: 550

Rep: Reputation: 26
Smile log file grep by date and year


hi guys, how do i use grep on a log file to filter by date and year.
Thanks for any input.
 
Old 01-08-2013, 08:50 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
You need to know the entry and date format in use then write your regex accordingly, grep supports multiple pattern arguments so you'd do something like:

Code:
grep -e <year_regex> -e <day_of_month_regex> /path/to/logfile
Any output will have matched both patterns.
 
Old 01-08-2013, 09:50 PM   #3
JJJCR
Member
 
Registered: Apr 2010
Posts: 550

Original Poster
Rep: Reputation: 26
Cool

Quote:
Originally Posted by kbp View Post
You need to know the entry and date format in use then write your regex accordingly, grep supports multiple pattern arguments so you'd do something like:

Code:
grep -e <year_regex> -e <day_of_month_regex> /path/to/logfile
Any output will have matched both patterns.
Thanks kbp, I will work on the regex and post the result here..


if i am on the directory where the log file is located, do I need to specify the path or just the name of the file?
 
Old 01-08-2013, 10:04 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Whichever you prefer .. normally I would keep scripts in a central location say /usr/local/bin, then use the absolute path.
 
Old 01-09-2013, 03:38 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,271

Rep: Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028Reputation: 1028
You may find this post unSpawn illuminating.
Date formats vary, but the lesson is there.
 
Old 01-14-2013, 03:49 AM   #6
JJJCR
Member
 
Registered: Apr 2010
Posts: 550

Original Poster
Rep: Reputation: 26
hi guys, I had tried the regex but it just doesn't seem to work.

actually i'm trying to find an exact match for the date 14 Jan 2013, using the commands below:

grep '^[14]/Jan/[2013]'+/ path to log

grep '^\(14\)?\(Jan\)?\(2013\)' path to log

grep '^[14]?[Jan]?[2013]*' path to log

log file has this format:

[14/Jan/2013 16:38:51] IP address 1.2.3.4

How to do it in regex to find the exact date match?

Thanks.
 
Old 01-14-2013, 03:54 AM   #7
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Code:
grep -e '^\[14\/Jan\/2013.*' /path/to/log
 
Old 01-14-2013, 04:16 AM   #8
JJJCR
Member
 
Registered: Apr 2010
Posts: 550

Original Poster
Rep: Reputation: 26
Smile

Quote:
Originally Posted by kbp View Post
Code:
grep -e '^\[14\/Jan\/2013.*' /path/to/log
Wow..thank you... it works 100%

sorry the dot after 2013 followed by * what does it mean?

2013. <--this dot what it means?
 
Old 01-14-2013, 05:19 AM   #9
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 778
Blog Entries: 2

Rep: Reputation: 199Reputation: 199
The final .* of the regex is pointless - dot means any character and * means zero or more repetitions of dot. You can end this RE at 2013
 
Old 01-14-2013, 05:32 PM   #10
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
True .. I think I put it in due to habit, commonly matching different pieces of a line
 
Old 01-14-2013, 09:24 PM   #11
JJJCR
Member
 
Registered: Apr 2010
Posts: 550

Original Poster
Rep: Reputation: 26
hi guys, thank you so much for all your help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search date period in access log using grep bilalcochin Linux - Newbie 2 02-19-2010 08:24 AM
Script to grep log files based on system date cyclegar Programming 2 05-05-2009 06:08 PM
How to write function to list file (*.log) follow feature time(date, month, year) phanvinhgiap Programming 2 12-30-2008 08:30 PM
Log File Date Oddness iamryman Linux - Server 1 05-22-2007 01:39 AM
Date in Log file name Runningonair Linux - General 8 06-02-2006 09:45 AM


All times are GMT -5. The time now is 03:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration