Date conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date conversion
# 15  
Old 02-11-2009
Quote:
Originally Posted by nickrick
vgersh99- wonder if your still around? How would you alter your awk script to process the date if it appeared in the first field of a comma delimited file? i.e

JAN 03 2009 05:30:00:PM,data1,data2,data3

to

2009/01/03 17:30:00,data1,data2,data3

sorry, probably a very simple question?
Code:
BEGIN {
  FS=OFS=","
  monN=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", months, " ")
  for(i=1; i<=monN; i++) {
    months[months[i]]=i
    delete months[i]
  }
}

function conv2mil(time,   tA, tAnum, pm_am) {
  tAnum=split(time, tA, ":")
  pm_am=toupper(substr(tA[tAnum], length(tA[tAnum])-1))
  hour= (pm_am ~ /^P./) ? tA[1] + 12 : tA[1]
  return (hour ":" tA[2] ":" tA[3])
}

{ timeAn = split($1, timeA, " ")
  $1 = sprintf("%s/%02d/%s %s",  timeA[3], months[toupper(timeA[1])], timeA[2], conv2mil(timeA[4]) )
  print
}

# 16  
Old 02-11-2009
Thanks vgersh99, that worked great.

so sprintf can be used to assign strings to vars and in awk you can reassign new strings to the field vars.

maybe I should move this to a different forum, but I've just hit an error because one of the input lines is longer than 3000 bytes. Is there anyway of getting around this? or am i going to have to truncate the string?
# 17  
Old 02-11-2009
Quote:
Originally Posted by nickrick
maybe I should move this to a different forum, but I've just hit an error because one of the input lines is longer than 3000 bytes. Is there anyway of getting around this? or am i going to have to truncate the string?
If you have 'gawk', try that instead of awk/nawk.
If you're on Solaris, try /usr/xpg4/bin/awk

YMMV
# 18  
Old 02-11-2009
I don't have gawk.

In this instance I'm not too bothered about the occasional line being corrupted so i'm just using 'cut -c1-3000' as a workaround.

Thanks again.
# 19  
Old 05-06-2009
Bug

Quote:
Originally Posted by vgersh99
one way.......

echo 'MAY 05 2005 01:15:00PM' | awk -f mab.awk

mab.awk:
Code:
BEGIN {
  monN=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", months)
  for(i=1; i<=monN; i++) {
    months[months[i]]=i;
    delete months[i];
  }
}
 
{ printf("%s/%02d/%s %s\n",  $3, months[toupper($1)], $2, $3) }



Hi vgersh99 - hope you're still here Smilie i saw this thread and it really helped me a lot! however i'm having difficulty now figuring out how to convert this date format (May 5 13:01) to yyyymmddhhmm appending the current year (2009).

for sample: (May 5 13:01) will be transformed to 200905051301

the reason i want to have it this way is in order for me to take the time difference of two variables.

I've tried modifying your script by using sprintf, but i failed to make it work Smilie

Hope you can help me with this Smilie

Many thanks!
# 20  
Old 05-06-2009
Quote:
Originally Posted by aller_in
Hi vgersh99 - hope you're still here i saw this thread and it really helped me a lot! however i'm having difficulty now figuring out how to convert this date format (May 5 13:01) to yyyymmddhhmm appending the current year (2009).

for sample: (May 5 13:01) will be transformed to 200905051301

the reason i want to have it this way is in order for me to take the time difference of two variables.

I've tried modifying your script by using sprintf, but i failed to make it work

Hope you can help me with this

Many thanks!
Code:
BEGIN {
  monN=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", months)
  for(i=1; i<=monN; i++) {
    months[months[i]]=i;
    delete months[i];
  }
}

{ gsub(":", "", $3);printf("2009%02d%02d%04d\n",  months[toupper($1)], $2, $3) }

# 21  
Old 05-06-2009
Tools

Quote:
Originally Posted by vgersh99
Code:
BEGIN {
  monN=split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", months)
  for(i=1; i<=monN; i++) {
    months[months[i]]=i;
    delete months[i];
  }
}
 
{ gsub(":", "", $3);printf("2009%02d%02d%04d\n",  months[toupper($1)], $2, $3) }


And it worked PERFECTLY!!!!
Thanks for your help GURU in Unix! Smilie Appreciate it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date conversion

Hi , we have a string with yyyymmdd format . how to know which date it is ? example:20120712-->sunday 20150228-->saturday 20140431-->invalid please suggest commands which work on below os : SunOS 5.10 shell: bash shell Thanks, Srinath. (10 Replies)
Discussion started by: srinadhreddy27
10 Replies

2. Shell Programming and Scripting

Date conversion

Trying to convert dates using a Perl Script but it has to accept formats like 3 letter month, day and year like Nov 02 2010 or 1/4/11 or 21 Feb 2011 and have it convert to something like October 20, 2011. Any ideas? (2 Replies)
Discussion started by: reduxeffect81
2 Replies

3. Shell Programming and Scripting

Julian date to Calendar date conversion

Hi all, I require to convert julian date to normal calander date in unix for eg julian date=122 now i want corresponding calander date ---------------------------------------- gr8 if give very small command/script and please explain the steps as well(imp) Thanks ... (3 Replies)
Discussion started by: RahulJoshi
3 Replies

4. UNIX for Dummies Questions & Answers

Date conversion in ab i

(string(8)) ((date("YYYYMMDD")) ((date("YYYY/MM/DD")) in.date_field_name)) (1 Reply)
Discussion started by: dr46014
1 Replies

5. Shell Programming and Scripting

Conversion of date to Julian date

Hi Gurus, Need help in Conversion of date(2007-11-30) to Julian date(YYDDD)... '+%J' 2007-11-30 to 'YYDDD' Thanks (4 Replies)
Discussion started by: SeenuGuddu
4 Replies

6. Shell Programming and Scripting

Date conversion

Hi, I have the string YYYYMMDDHHMMSS like 20090801204150 and I need to convert it using the unix command date in the format: date "Saturday, 1 August 2009 20:40:59" All in one single Unix line if this is possible. What's the correct syntax? Steve Hagi (6 Replies)
Discussion started by: hagimeno
6 Replies

7. Shell Programming and Scripting

Date Conversion

Hi, Does anyone know (in KSH, CSH, SED or AWK), how to convert date text in a file from: EX: May232008 to: 05232008 Thanks, (3 Replies)
Discussion started by: jgrant746
3 Replies

8. Shell Programming and Scripting

date conversion

file1 E106,0,1/9/1993,0,E001,E003,A,45200,3766.667,21.730769 E108,0,2/3/1995,0,E001,E003,A,15000,1250,7.211538 E109,0,06-mar-07,0,E001,E001,A,78000,6500,37.5 E110,0,09-dec-2008,0,E001,E001,A,56000,4666.667,26.923077 E104,0,06/04/1994,0,E001,E003,A,95000,7916.667,45.673077... (14 Replies)
Discussion started by: charandevu
14 Replies

9. Shell Programming and Scripting

date conversion

Hi everybody: Could anybody tell me how I convert from a julian date, with shell comands, to gregorian. Thanks in advance. (2 Replies)
Discussion started by: tonet
2 Replies

10. UNIX for Advanced & Expert Users

Date Conversion

Hello, I want to convert MM DD YYYY date format to MM-DD-YYYY format. For exemple: I have to convert Nov 28 2005 to 28-11-2005. Thenks for youf help. DAFI (2 Replies)
Discussion started by: dafidak
2 Replies
Login or Register to Ask a Question