Convert date with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert date with awk
# 1  
Old 03-25-2010
Convert date with awk

Hi all,

I'm fighting with awk because I didn't find any way with other commands Smilie

I need to convert a date from format A to format B :
format A : "YYYY MM DD"
format B : "DD-MON-YYYY"

Exemple : "2010 3 25" => "25-MAR-2010"

My main difficulty is to convert the month with something like that :
Code:
split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month, " ")

Help me please...
# 2  
Old 03-25-2010
Code:
$ echo '2010 3 25' | nawk 'BEGIN{split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month, " ")} {print $3,month[$2],$1}' OFS=-
25-MAR-2010

# 3  
Old 03-25-2010
Perfect !

month[$2] of course Smilie

Many thanks
# 4  
Old 04-05-2010
How do I go about doing the reverse of this? Say I have a date in the

format A:
APR 02, 2010

and want it converted to

format B:
04/02/2010

??

Thanks,
Bharath
# 5  
Old 04-05-2010
Code:
echo 'APR 02, 2010' | nawk 'BEGIN{FS=FS "|,";split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", month, " ");while(++i in month) m[month[i]]=i} {printf("%02d%c%02d%c%d\n", m[$1],OFS,$2,OFS,$NF)}' OFS='/'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk command to convert number occurances into date format and club a set of lines

Hi, I have been stuck in this requirement where my file contains the below format. 20150812170500846959990854-25383-8.0.0 "ABC Report" hp96880 "4952" 20150812170501846959990854-25383-8.0.0 End of run 20150812060132846959990854-20495-8.0.0 "XYZ Report" vg76452 "1006962188"... (6 Replies)
Discussion started by: Chinmaya Kabi
6 Replies

2. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

3. Shell Programming and Scripting

Convert date arg to sql date

Doing a bcp load to sybase and need to convert datearg which comes in as 20130501 to 2013-05-01 which is the best way to do this (4 Replies)
Discussion started by: tasmac
4 Replies

4. Shell Programming and Scripting

awk convert date format

Could you tell me how to convert the following dates? If I have m/d/yyyy, I want to have 0m/0d/yyyy. I want my dates to always be 8 digits. In other words, I want a 0 inserted whenever the month or day is a single digit. My issue is first I need to use FS="," to get field $4 for the... (7 Replies)
Discussion started by: wbrunc
7 Replies

5. UNIX for Dummies Questions & Answers

Unable to convert date into no. using date -d +%s syntax in ksh shell

hi friends, I m trying to write a script which compares to dates. for this i am converting dates into no using synatx as below v2=`date | awk '{print $2,$3,$4}'` v3=`date +%s -d "$v2"` this syntax is working in bash shell ,but fails in ksh shell. please suggest on this. (12 Replies)
Discussion started by: Jcpratap
12 Replies

6. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

7. Shell Programming and Scripting

Awk convert from day of year to date

Hello everyone, I have a file which has day of year in one of the columns (JD=substr($0,72,3)). The bellow scripts shows me the minimum and maximum values of the JD and I would like to convert the JD to date. #!/bin/gawk -f { check=substr($0,1,1) if (check == "S") { ... (6 Replies)
Discussion started by: alex2005
6 Replies

8. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

9. Shell Programming and Scripting

convert Julian date to calender date

Hi, I have script in unix which creates a julian date like 126 or 127 I want convert this julian date into calender date ex : input 127 output 07/may/2007 or 07/05/2007 or 07/05/07 rgds srikanth (6 Replies)
Discussion started by: srikanthus2002
6 Replies
Login or Register to Ask a Question