Using awk to convert DD-MMM-YY to YYYYMMDD


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to convert DD-MMM-YY to YYYYMMDD
# 1  
Old 09-15-2008
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  
Old 09-15-2008
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  
Old 09-15-2008
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  
Old 09-15-2008
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 20070701Smilie

Any suggestions??
# 5  
Old 09-15-2008
Use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 6  
Old 09-15-2008
Great!!!! It works!!!Smilie

ThanksSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Convert string (YYYYMMDD) format to date in Sun OS

Hi All I need help in converting a string of YYYYMMDD format to date in Sun OS and then find out if the day is a Wednesday or not. The "date -d" option is not working and your help is much appreciated. The date command usage from the operating system we use here is as follows: Thanks, SK (11 Replies)
Discussion started by: SK123
11 Replies

2. Shell Programming and Scripting

Convert string (YYYYMMDD) format to date in Sun OS

Hi All I need help in converting a string of YYYYMMDD format to date in Sun OS and then find out if the day is a Wednesday or not. The "date -d" option is not working and your help is much appreciated. The date command usage from the operating system we use here is as follows: usage: ... (1 Reply)
Discussion started by: SK123
1 Replies

3. UNIX for Advanced & Expert Users

How to convert mmm-yy to mm/dd/yyyy format in UNIX ?

How to convert mmm-yy to mm/dd/yyyy format in unix ? example: Jan-99 to 01/01/1999 Jan-00 to 01/01/2000 Jan-25 to 01/01/2025 Dec-99 to 01/12/1999 Dec-00 to 01/12/2000 Dec-25 to 01/12/2025 YY anything between 00-50 should be 2000-2050 YY anything between 51-99 should be 1951-1999 ... (2 Replies)
Discussion started by: gksenthilkumar
2 Replies

4. Shell Programming and Scripting

Date conversion DD-MMM-YY to YYYYMMDD for many columns in a file

Hi, Input Col1|col2|col3|col4|col5|col6-------col26 1|2|3|10-Nov-67|10-Nov-97|4|5-------100 1|2|3|11-Feb-01|01-Dec-15|4|5-------2000 Output Col1|col2|col3|col4|col5|col6-------col26 1|2|3|19671110|19971010|4|5-------100 1|2|3|20010211|20151201|4|5-------2000 I want to convert... (3 Replies)
Discussion started by: onesuri
3 Replies

5. Shell Programming and Scripting

reformat HHMMSSmmm into HH:MM:SS.mmm

I have a bunch of logs where the timestamp hours, minutes, seconds and milliseconds all together without a visual delimiter, as below 123045000 I'd prefer to see more easier to read familiar format, as 12:30:45.000 I came up with this ugly echo '123045000'|awk '{print... (2 Replies)
Discussion started by: migurus
2 Replies

6. AIX

Convert time (YYYYMMDD HHMMSS) to UTC

Okay, so let's say we have a string like: 20110105_193345 This represents: January 5th, 2011 = 20110105 24-hour style time 19:33:45 = 193345 Okay, so we have our time. It's January 5th, 2011 at 19:33:45. I want to convert this time from Eastern Time Zone (which it currently is in)... (1 Reply)
Discussion started by: syndex
1 Replies

7. Shell Programming and Scripting

convert date format YYYYMMDD to MM/DD/YYYY

In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY. Thanks. (8 Replies)
Discussion started by: nasirgondal
8 Replies

8. Shell Programming and Scripting

Convert DD-MMM-YYYY to MM-DD-YYYY

I have a file which has 100k+ records like this abc,05-JUN-1974,def,lkj,aaa def,11-SEP-1975,ghj,dis,dea I want to convert ex 05-JUN-1974 to 06/05/1974 Please help me with awk script to convert the whole file into MM-DD-YYYY Thank you! (2 Replies)
Discussion started by: nuthalapati
2 Replies

9. UNIX for Dummies Questions & Answers

how to convert the string YYYYMMDD into YYYY.MM.DD

how to convert the string YYYYMMDD into YYYY.MM.DD Please advice (1 Reply)
Discussion started by: spatra
1 Replies

10. Shell Programming and Scripting

Convert filenames with DDMMYYYY to YYYYMMDD

Okay, I have posted on here one other time and received very fast help, so I figured I'd post again. Searched for awhile and couldn't find exactly what I'm looking for. I am attempting to write a script that will search through a given directory, or search through the current directory, and... (10 Replies)
Discussion started by: cbo0485
10 Replies
Login or Register to Ask a Question