The UNIX and Linux Forums  

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
convert date format YYYYMMDD to MM/DD/YYYY nasirgondal Shell Programming and Scripting 8 04-08-2009 11:56 AM
how to convert the string YYYYMMDD into YYYY.MM.DD spatra UNIX for Dummies Questions & Answers 1 07-23-2008 08:18 AM
Convert filenames with DDMMYYYY to YYYYMMDD cbo0485 Shell Programming and Scripting 10 04-24-2008 01:48 PM
get yesterday in yyyymmdd format aaron_fong Shell Programming and Scripting 13 03-14-2008 03:14 PM
Converting YYYYMMDD to Julian dfran1972 Shell Programming and Scripting 5 04-28-2005 11:34 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 09-15-2008
shanu_85 shanu_85 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
Using awk to convert DD-MMM-YY to YYYYMMDD

Hi
i need to convert a date in the format DD-Mon-YY to YYYYDDMM

Ex : 01-JUL-00 to 20000701

Can anybdy help me with this??

Thanks in advance
Shenaz
  #2 (permalink)  
Old 09-15-2008
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Hi shanu using '-' as delimiter u can awk but i am not sure how can we directly convert "JUL" to 07. might be you need to place all the months and their numbers in a file first and awk.
  #3 (permalink)  
Old 09-15-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,350
An example how to convert the date format DD-MMM-YY to YYYYMMDD assuming the century is 2000 if the year is in 2 digits:


Code:
awk -v var="01-JUL-00" '
BEGIN{
  split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month, " ")
  for (i=1; i<=12; i++) mdigit[month[i]]=i
  m=toupper(substr(var,4,3))
  dat="20"substr(var,8,20)substr(var,1,2) sprintf("%02d",mdigit[m])
  print dat
}'

Regards
  #4 (permalink)  
Old 09-15-2008
shanu_85 shanu_85 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
awk -v does nt seem to work.

awk -v var="01-JUL-00" '
BEGIN{
split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month, " ")
for (i=1; i<=12; i++) mdigit[month[i]]=i
m=toupper(substr(var,4,3))
dat="20"substr(var,8,20)substr(var,1,2) sprintf("%02d",mdigit[m])
print dat
}'
awk: syntax error near line 1
awk: bailing out near line 1

So i tried this

echo $var | awk 'BEGIN{FS="-";OFS="";split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month, " ")
for (i=1; i<=12; i++) mdigit[month[i]]=i
m=toupper(substr(var,4,3))
dat="20"substr(var,8,20)substr(var,1,2) sprintf("%02d",mdigit[m])
print dat}'

and the output is 2000 and not 20070701

Any suggestions??
  #5 (permalink)  
Old 09-15-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,350
Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
  #6 (permalink)  
Old 09-15-2008
shanu_85 shanu_85 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 5
Great!!!! It works!!!

Thanks
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 07:49 AM.


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