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!

using grep for a range of files

Status
Not open for further replies.

jalge2

Technical User
Feb 5, 2003
105
US
Hi all. I'm trying to grep for a range of files.

The file names are wavelog.20031005 through wavelog.20031008.

My script looks like thisrm /dso/mis/scripts/labelcounts/1log.txt

print "Would you like to get counts for only 1 day? (y/n) \c"
read ans


if [ $ans = "y" -o $ans = "Y" ]
then
print "Please enter the date you wish to search... (yyyymmdd) \c"
read Date
print "Please enter the printer you wish to search for... \c"
read prt

if [ /dso/eis/log/labelcounts.* = /dso/eis/log/labelcounts.$Date ]
then
grep $prt /dso/eis/log/labelcounts.$Date >> /dso/mis/scripts/labelcounts/1log.txt

grep $prt /dso/eis/log/wavelog.$Date >> /dso/mis/scripts/labelcounts/1log.txt

else
grep $prt /dso/eis/log/wavelog.$Date >> /dso/mis/scripts/labelcounts/1log.txt

fi
fi
if [ $ans = "n" -o $ans = "N" ]
then
print "Would you like to search for a range of dates ... (y/n) \c"
read ans1
if [ $ans1 = "y" -o $ans1 = "Y" ]
then
print "Please enter the first date ... (yyyymmdd) \c"
read Date1
print "Please enter the second date ... (yyyymmdd) \c"
read Date2



else
exit


fi
fi

So my first date is Date1 and my second date is Date2. How can I tie in grep to search files between the two dates I enter?

Thanks for the help.

jalge2
 
[tt]grep $(ls wavelog.* | awk -F. '$2 >= '$Date1' && $2 <= '$Date2'')[/tt]

Annihilannic.
 
[tt]grep searchstring $(ls wavelog.* | awk -F. '$2 >= '$Date1' && $2 <= '$Date2'')[/tt]

It would help if I included the searchstring too! :)

Annihilannic.
 
To do a range, you could do something like this...
Code:
ls -1 wavelog.* | sort | sed &quot;/$Date1/,/$Date2/p;d&quot; | xargs grep $prt
The only problem with this is that the two dates MUST exist as parts of filenames. If the first one is missing, you'll get nothing. If the second one is missing, you'll get from the first one to the very last filename.

Maybe just add a validation checking for the file's existence before doing it.

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top