The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get date and time for past 1 hour from current date spch2o Shell Programming and Scripting 5 08-29-2008 05:32 AM
help in time format ali560045 Shell Programming and Scripting 3 05-28-2008 07:10 AM
Processing a log file based on date/time input and the date/time on the log file primp Shell Programming and Scripting 4 03-16-2008 11:23 AM
date issue-find prevoius date in a patricular format bsandeep_80 UNIX for Advanced & Expert Users 3 11-15-2007 08:42 PM
convert mmddyy date format to ccyyddd format?? Bhups Shell Programming and Scripting 2 09-28-2006 12:30 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-17-2002
apalex apalex is offline
Registered User
  
 

Join Date: May 2001
Location: NY
Posts: 39
format date/time presentation

1) i am trying to print the timeframe of a log file using the start/end date/time being extracted from the file itself. my problem is how to convert the following numeric date format to the one that i prefer:

Start time: 0204161129
End time : 0204171431

into:

Start time: Apr 16 2002 11:29 AM
End time : Apr 17 2002 14:31 PM

I have a script that is working, but with a horrible 40+ lines into it, and i wanted to, at least, improve it.

2) also, for the "man date" description below, how can i get all those variables, and the value they represent? is there a command # date '+....' for a variable to specify AM or PM on the date/time? i think this will be a very good info too.

thanks a lot!
==============================================
DESCRIPTION
The date utility writes the date and time to standard output
or attempts to set the system date and time. By default,
the current date and time will be written.

Specifications of native language translations of month and
weekday names are supported. The month and weekday names
used for a language are based on the locale specified by the
environment variable LC_TIME; see environ(5).

The following is the default form for the "C" locale:

%a %b %e %T %Z %Y
  #2 (permalink)  
Old 04-18-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
1) It's not very clear where the century is supposed to come from. But I'll assume that high values get a 19 while low values get a 20.
Code:
#! /usr/bin/ksh

input=0204161309

set -A months Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
echo $input | sed 's/\(..\)/\1 /g' | read yr mo dy hr mn
((yr<50)) && cn=20 || cn=19
((hr<12)) && ss=am || ss=pm
((hr=hr%12))

print  ${months[mo-1]} $dy ${cn}${yr} ${hr}:${mn} $ss
2) "man strftime" has the complete list. And
date "+%p"
will output am or pm.
  #3 (permalink)  
Old 04-18-2002
apalex apalex is offline
Registered User
  
 

Join Date: May 2001
Location: NY
Posts: 39
i'm getting this error on line 18. also, do i have to put $ sign on $hr and $mo, considering they are variables?
what does ${months[$mo-1]} do with reference to the sed command?

thanks...

15 (($hr<12)) && ss=AM || ss=PM
16 ($hr=$hr%12))
17
18 print ${months[$mo-1]} $dy $yr ${hr}:${mn} $ss


[18]: months: subscript out of range
  #4 (permalink)  
Old 04-18-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
I cut and pasted that script in after testing it. Don't fiddle with it and it will work. It it still fails after you put it back the way it is supposed to be, put a line
echo mo = $mo
just before the print and post the entire script as you are running it and the results of the echo mo.
  #5 (permalink)  
Old 04-18-2002
apalex apalex is offline
Registered User
  
 

Join Date: May 2001
Location: NY
Posts: 39
Thanks, i had to cut & paste the script into notepad then into my tty. It's working.
  #6 (permalink)  
Old 04-19-2002
apalex apalex is offline
Registered User
  
 

Join Date: May 2001
Location: NY
Posts: 39
Perderabo, can you please provide explanation on the text in bold ?
I would like to have a concrete analysis of the script so i can use them
for in the future? Thanks...

#! /usr/bin/ksh

input=0204161309

set -A months Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
echo $input | sed 's/\(..\)/\1 /g' | read yr mo dy hr mn
((yr<50)) && cn=20 || cn=19
((hr<12)) && ss=am || ss=pm
((hr=hr%12))

print ${months[mo-1]} $dy ${cn}${yr} ${hr}:${mn} $ss
  #7 (permalink)  
Old 04-19-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
The sed command put a space after every two characters, this is how I split the fields up so that I could then read them into seperate variables.

Hmmm...now that I look at it, the hr thing may need some help. First hr holds the hours and if the hr is less than 12, it fine the way it is. But if hr was 13, we need it to be set to 1. And if it is 14, we want 2. This is so we can move from a 24 hour clock to a 12 hour clock. The % operator does this. It is a integer divide, but the result is the remainder, not the quotient. So 14 % 2 means we divide 14 by 12 and we want the remainder which is 2.

The problem that I now see is that if hour is zero, we may want to set it to twelve. This kinda depends on your local customs and whatnot, but this is how my watch works. We can do that with:

((hr=hr%12)) || hr=12

The || does the second command only if the first command fails. And a ((...)) that results in a zero is treated as if it failed. I just love stuff like this.

The set command established an array of 12 elements, one for each month. But the index runs from zero to 11. So I had to subtract 1 from the mo variable to get the proper index. So that expression just pulls out the proper element from the array.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0