Getting previous hour in formatted form | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Getting previous hour in formatted form

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-05-2011
Amutha Amutha is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 September 2013, 2:50 AM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
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

Code:
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.

Last edited by radoulov; 04-05-2011 at 10:41 AM.. Reason: Code tags, please!
Sponsored Links
    #2  
Old 04-05-2011
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
 
Join Date: Mar 2007
Last Activity: 21 June 2012, 3:19 PM EDT
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 103 Times in 100 Posts
If you have the file names already in a file and they are sorted:

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

Sponsored Links
    #3  
Old 04-05-2011
Chubler_XL's Avatar
Chubler_XL Chubler_XL is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 18 December 2014, 5:15 PM EST
Posts: 3,051
Thanks: 114
Thanked 984 Times in 921 Posts
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.


Code:
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


Last edited by Chubler_XL; 04-05-2011 at 01:13 PM..
    #4  
Old 04-05-2011
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 16 December 2014, 5:07 AM EST
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts
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.
Sponsored Links
    #5  
Old 04-05-2011
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 25 November 2014, 5:09 PM EST
Posts: 6,396
Thanks: 287
Thanked 673 Times in 642 Posts
Quote:
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"
Why is the parameter value "6" ?
Do you mean:
Quote:
2011-04-05-20
2011-04-05-21
2011-04-05-22
2011-04-05-23
2011-04-06-00
2011-04-06-01
Please list all the files you would expect to search when the parameter is value "6".

Last edited by methyl; 04-05-2011 at 01:36 PM.. Reason: Date format
Sponsored Links
    #6  
Old 04-05-2011
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 16 December 2014, 5:07 AM EST
Location: France
Posts: 2,930
Thanks: 81
Thanked 624 Times in 596 Posts
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 ?
Sponsored Links
    #7  
Old 04-06-2011
Amutha Amutha is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 10 September 2013, 2:50 AM EDT
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by methyl View Post
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".

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 ----------

Quote:
Originally Posted by Chubler_XL View Post
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.


Code:
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


Hi Chuble,

Thank you so much.. i got what i need..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Show previous hour alvin0618 Shell Programming and Scripting 15 04-05-2011 11:56 AM
Hour by Hour counting script. Where do I start? dahelgran Shell Programming and Scripting 4 07-12-2009 07:27 PM
an hour less in 24 hour system bobo UNIX for Dummies Questions & Answers 3 10-23-2006 12:55 PM
Changing Unix form to Microsoft Word form to be able to email it to someone. Cheraunm UNIX for Advanced & Expert Users 8 05-24-2002 04:58 AM



All times are GMT -4. The time now is 08:43 AM.