nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

How to grep the date range

This is a discussion on How to grep the date range within the Shell scripting forums, part of the Development/Scripting category; How we can use the grep command to search for a particular date range e.g. need to grep the data ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 05-12-2008, 10:10 PM
Junior Member
User
 
Join Date: May 2008
My distro: Debian
Posts: 8
Rep Power: 0
sainipardeep is on a distinguished road
Default How to grep the date range

How we can use the grep command to search for a particular date range e.g. need to grep the data from 25 April to 3 May.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-13-2008, 05:19 PM
agn agn is offline
Member
User
 
Join Date: Feb 2008
My distro: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Rep Power: 1
agn is on a distinguished road
Default

You can use sed to do that. You can't use a date/time range but you can specify the pattern. Assuming you data looks something like this...

Apr 25 something ... .. ....
Apr 25 sss ww aa....
Apr 26......
.
.
.
May 3 bbb ccc ddd...

Code:
sed -n '/Apr 25/,/May 3/p' input_file
Reply With Quote
  #3 (permalink)  
Old 05-13-2008, 05:28 PM
Junior Member
User
 
Join Date: May 2008
My distro: Debian
Posts: 8
Rep Power: 0
sainipardeep is on a distinguished road
Default Thanks AGN

Hi

Thanks for ur reply but thr is sm method that i dont know

suppose u want to grep frm 20 to 25th u can do easily using:

grep -n "2[0-5]/April/2008" input_file

but problem is if the month is different
Reply With Quote
  #4 (permalink)  
Old 05-13-2008, 05:38 PM
agn agn is offline
Member
User
 
Join Date: Feb 2008
My distro: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Rep Power: 1
agn is on a distinguished road
Default

Yep, you can also use grep with regex.

Code:
egrep  '2[0-5]/(April|May)/2008' logfile
Reply With Quote
  #5 (permalink)  
Old 05-13-2008, 05:41 PM
Junior Member
User
 
Join Date: May 2008
My distro: Debian
Posts: 8
Rep Power: 0
sainipardeep is on a distinguished road
Default Plz elaborate

egrep '2[0-5]/(April|May)/2008' logfile this ma work but could u plz elaborate if the date range is 28 april to 02 may
Reply With Quote
  #6 (permalink)  
Old 05-13-2008, 05:53 PM
agn agn is offline
Member
User
 
Join Date: Feb 2008
My distro: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Rep Power: 1
agn is on a distinguished road
Default

Code:
egrep  '((2[8-9]|3[0-1])/April|0[1-2]/May)/2008' logfile
If your data is ordered you might wanna use the sed instead of grep as the regex may get complex depending on your requirements.
Reply With Quote
  #7 (permalink)  
Old 05-13-2008, 06:06 PM
Junior Member
User
 
Join Date: May 2008
My distro: Debian
Posts: 8
Rep Power: 0
sainipardeep is on a distinguished road
Default Some problem

1. egrep '(2[8-9]|3[0])/April/2008' server-81-access_log

If we try this command there will be no output.

2. egrep '(2[8-9]|3[0])/April/2008|0[1-2]/May/2008' server-81-access_log

if we try this command there is output but for 1st n 2nd may only not for the april dates.......
Reply With Quote
  #8 (permalink)  
Old 05-13-2008, 06:19 PM
agn agn is offline
Member
User
 
Join Date: Feb 2008
My distro: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Rep Power: 1
agn is on a distinguished road
Default

You didn't specify the access log format any where in your posts. And the above examples where meant to give you some hint.
Reply With Quote
  #9 (permalink)  
Old 05-13-2008, 06:23 PM
Junior Member
User
 
Join Date: May 2008
My distro: Debian
Posts: 8
Rep Power: 0
sainipardeep is on a distinguished road
Default Format

date format is 20/April/2008

I don understand y the | operator is not working.

and I m sorry i dont have any idea about sed thats y i m putting stress on grep only
Reply With Quote
  #10 (permalink)  
Old 05-13-2008, 06:27 PM
agn agn is offline
Member
User
 
Join Date: Feb 2008
My distro: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Rep Power: 1
agn is on a distinguished road
Default

Give me some lines from your log file. And are you sure that your log contains records for April 28, 29 and 30 ?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
Shell date command for manipulation and formatting date newbie4 Shell scripting 1 01-30-2008 01:21 PM
Grep ip address in Linux or UNIX sweta Getting started tutorials 0 07-27-2007 06:47 AM
Linux sync. out of range monitor error harshala09 Linux hardware 2 06-14-2007 09:37 AM
Grep and remove files Linux software 1 01-05-2006 06:25 PM


All times are GMT +5.5. The time now is 04:40 AM.


Powered by vBulletin® Version 3.7.3 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36