Hello I would like to ask for help with a script to search a directory that contains many log files and based on a users input after being prompted, they enter a date range down to the hour which searches the files that contain that range.
I dont know how to go about this. I am hoping that the user would be prompted by entering the hour to search but that wont work when the day changes and you have a number of files of separate days but same hour.
Would the user need to enter a variable for each date component "enter year, enter month, enter day, enter hour and then the script searches the best range of files
XSLog$YYYY.$MM.$dd-$hh.01.00.txt - if this is the best way how could this be done based on the log file format below?
#! /usr/bin/ksh
/var/logs/
ls -ltr
XSLog2012.03.28-09.20.00.txt XSLog2012.03.28-09.32.00.txt
XSLog2012.03.28-09.49.00.txt XSLog2012.03.28-10.01.00.txt
#prompt user for start date and time
echo you are about to search filename based time range of an hour"
echo Time year month day hour start = " read stime
echo Time year month day hour stop = " read etime
# have left out rest of the action on the file that strips out data
Thanks for the site. That find command is very handy and powerful but I am not sure I can use it in my case.
Some problems I have are.
-That the directory contains upto 2 weeks of files.
-Can contain other file types and I only want it to search the XSLogxxxxx files.
-The user will type in a day and hour to search on.
-From that user input, the script then searches the small range of files based on date and hour for whatever data they need.
I was thinking of using the user input after being prompted for "year" "month" "day" and "hour" and using the variables of those to produce the file range to search on.
echo enter year: "
read YYYY
echo enter month: "
read MM
echo enter day: "
read DD
echo enter Hour: "
read HH
sed -e '/./{H;$!d;}' -e 'x;/1234/!d' XSLog$YYYY.$MM.$DD-$HH*
I have not tried this but would this be an approach ?
Thankyou for any advice better methods or examples
There's one thing in your favor -- YYYY MM DD HH MM SS dates sort alphabetically. ls will give you them in date order. Then all you do is an alphabetic < > comparison to see if they're in the range you want...
printf "Enter start YYYY/MM/DD HH "
# Should work for YYYY/MM/DD HH or YYYY MM DD HH
IFS="/ " read S_YYYY S_MM S_DD S_HH
printf "Enter end YYYY/MM/DD HH "
IFS="/ " read E_YYYY E_MM E_DD E_HH
EARLIEST="XSLog$S_YYYY.$S_MM.$S_DD-$S_HH.00.00.txt"
LATEST="XSLog$E_YYYY.$E_MM.$E_DD-$E_HH.59.59.txt"
ls -1 | awk -v E="$EARLIEST" -v L="$LATEST" '($0 >= E) && ($0 <= L)' | xargs sed -e '/./{H;$!d;}' -e 'x;/1234/!d'
Thanks very much, I will try this when I am next at work on Monday.
I was not aware of the IFS "/" so it was interesting to read your post.
Thanks again:b: