Getting previous hour in formatted form

The requirement is, i need to search for the logs for particular duration. The logs are in the format

logfilename.2011-04-05-00
logfilename.2011-04-05-01
logfilename.2011-04-05-02
logfilename.2011-04-05-03
.
.
.
.
logfilename.2011-04-05-18
.
.
.
logfilename.2011-04-05-23

the time is in 24 hr format that is being appended to the date to the filename.

i need to search a particular filed in the logs for a particular duration, so i have written a script. The script takes two parameters one as start time(only date-time) and duration.

ex: sh script.sh 2011-03-05-20 6

inside the script i need to start searching the filed rite from "logfilename.2011-04-05-20" to "logfilename.2011-04-06-03"

i'm planning to use for loop to generate the dates and append it to the string "logfilename" and process with that filename.

can you please help me out how to navigate the time in both reverse and forward.

If you have the file names already in a file and they are sorted:

#!/bin/ksh
# Usage: <script> From To
# Where: From and To have the format "YYYY-MM-DD-XX"
sed -n "/$1/,/$2/p" b2

If you have GNU date (accepts -d), this should do it.

Convert date+time to epoch time using %s format (seconds past epoch date).
Once you have the date+time as epoch time you can navigate forwards and backwards by just adding or subtracting the number of secs required (eg 3600 for 1 hour).
Use the GNU -d @<epochsecs> parameter to convert back to the display format.

FMT=$( echo $1 | sed -e 's/\(.*\)-\(.*\)/\1 \2:00/')
SECS=$(date -d "$FMT" +%s)
COUNT=$2
while [ $COUNT -gt 0 ]
do
   echo logfilename.$(date -d "@$SECS" +%Y-%d-%m-%H)
   let SECS=SECS+3600
   let COUNT=COUNT-1
done

which OS are you running ?

You would need to convert start date into seconds from 1970, and add to it the duration (converted in seconds)
(could be achieved using GNU date or using printf in ksh93, or maybe also using the Perderabo script , also read this thread)
You have then the ending date in second that you can convert back to a date so you can then scan the logfile and start printing lines as soon as first date has been reached and stop to print as soon as ending date has been reached.

Why is the parameter value "6" ?
Do you mean:

Please list all the files you would expect to search when the parameter is value "6".

My understanding is 6 stand for 6 hours so that's
from 2011-04-05-20
to 2011-04-06-01 (included)
(so entries raised from 2011-04-05 20:00 until 2011-04-06 01:59 should be displayed)

so stop grab log entries from 2011-04-06 02 ?

yes what you meant is rite. i need to get the list like this. the duration might be different and the date also might be different depending upon the user is passing it.

---------- Post updated 04-06-11 at 02:05 AM ---------- Previous update was 04-05-11 at 11:34 PM ----------

Hi Chuble,

Thank you so much.. i got what i need..