Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

greping in a date range

Status
Not open for further replies.

jalge2

Technical User
Feb 5, 2003
105
US
Hi all, I'm trying to grep for a certain battery number between files. The files are named batlog.log.date

date = (yyyymmdd) ex. 20030708

I'm not exactly sure how to grep for a certain number in between a said number of files. Here is what I have so far. print "\tPlease enter the battery number that you wish to search for (y/n)\c"
+6 read ans
+7
+8 print "\tPlease enter the first date you wish to search for (yyyymmdd)\c"
+9 read date1
+10
+11 print "\tPlease enter the second date you wish to search for (yyyymmdd)\c"
+12 read date2
+13
+14 if [[ $ans != 0 ]]
+15
+16 then

after this I seem to get a little stumped on how to attack this monster. Can someone help?


 
Assuming that the files have timestamps which reflect the date in the filename, like this
Code:
-rw-rw-r--    1 spc      spc             0 Jul  3 00:00 batlog.log.20030703

Then you could try this
Code:
touch -t ${date1}0000 oldest_file
touch -t ${date2}0000 newest_file

# find files between those two dates and search in them
find . -name "batlog.log.*" -newer oldest_file ! -newer newest_file -exec grep $ans {} \;

The find command has a large number of possibilities for finding files based on all sorts of criteria.
 
#!/usr/bin/ksh

Fdate=20030702
Ldate=20030704

ans=x

ls -1 bat.log* | while read -- File
do
[[ ${File##*.} -ge $Fdate && ${File##*.} -le $Ldate ]] &&
grep $ans $File /dev/null
done
 
Not tested, but worth a try...

filelist=$(ls -1 batlog.log.*|awk /$date1/,/$date2/)
grep $ans $filelist

 
Salem,

What if the time stamps are all at different times? They are a couple minutes different on each instance?
 
So long as the YYYYMMDD part of the filename is consistent with the timestamps, the HHMMSS of the timestamp wont matter.

If you want date1 to date2 inclusive, you might want
Code:
touch -t ${date2}2359.59 newest_file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top