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
Date Conversion jgrant746 Shell Programming and Scripting 3 05-24-2008 02:16 PM
Date Conversion MJDRM UNIX for Dummies Questions & Answers 5 01-29-2008 06:20 PM
date conversion tonet Shell Programming and Scripting 2 07-07-2006 10:23 AM
Date Conversion dafidak UNIX for Advanced & Expert Users 2 07-04-2006 05:16 PM
date conversion sunil bajaj UNIX for Dummies Questions & Answers 3 04-17-2002 01:54 PM

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 Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 02-11-2009
nickrick nickrick is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 13
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?
  #2 (permalink)  
Old 02-11-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by nickrick View Post
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
}
  #3 (permalink)  
Old 02-11-2009
nickrick nickrick is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 13
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?
  #4 (permalink)  
Old 02-11-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by nickrick View Post
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
  #5 (permalink)  
Old 02-11-2009
nickrick nickrick is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 13
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.
  #6 (permalink)  
Old 05-06-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
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) }
  #7 (permalink)  
Old 05-06-2009
aller_in aller_in is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Cool

Quote:
Originally Posted by vgersh99 View Post
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! Appreciate it!
Closed Thread

Bookmarks

Tags
linux, ubuntu

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 03:33 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