Converting Date from YYYYMMDD to DD-MON-YYYY


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting Date from YYYYMMDD to DD-MON-YYYY
# 8  
Old 12-15-2011
Try this...
Code:
echo "20111214" | nawk 'BEGIN{a[1]="JAN";a[2]="FEB";a[11]="NOV";a[12]="DEC"}
{match($0,"(....)(..)(..)",d);print d[3],a[d[2]],d[1]}' OFS="-"

of course, fill in the remaining months!
--ahamed
# 9  
Old 12-15-2011
Quote:
Originally Posted by ady_koolz
@vivek ... Thanks but its not working . It is giving bad substitution error.
I am a novice to scripting . Also I am using as Sun Solaris unix machine

---------- Post updated at 03:01 AM ---------- Previous update was at 02:59 AM ----------

@ni2 date command --date and -f options are not available on sun solaris machine
@ady_koolz
Sorry. Can't help with that.

@vivek d r
Thanks.
# 10  
Old 12-15-2011
Code:
 echo "20111214" | nawk 'BEGIN{split("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC",month," ")}{printf("%s-%s-%s\n",substr($0,7,2),month[substr($0,5,2)],substr($0,1,4))}'

# 11  
Old 12-15-2011
@itikamraj and ahamed

It is half working . If the date come as 20110612 then it does not show the month.Because I think month array does not recoznize 06.
# 12  
Old 12-15-2011
Try this...
Code:
echo "20111214" | nawk 'BEGIN{a[1]="JAN";a[2]="FEB";a[11]="NOV";a[12]="DEC"}
{match($0,"(....)(..)(..)",d);print d[3],a[d[2]+0],d[1]}' OFS="-"

--ahamed
# 13  
Old 12-15-2011
@ady_koolz: Try the below perl program. It'll even check if input date is in YYYYMMDD format.
perl code:
  1. #! /usr/bin/perl -w
  2. use strict;
  3.  
  4. (@ARGV != 1) && die "Provide only one parameter in YYYYMMDD format. Exiting";
  5.  
  6. my ($yr, $mt, $dt) = (0, 0, 0);
  7.  
  8. if ($ARGV[0] =~ /20[0-9]{2}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])/) {
  9.     $yr = substr $ARGV[0], 0, 4;
  10.     $mt = substr $ARGV[0], 4, 2;
  11.     $dt = substr $ARGV[0], 6, 2;
  12. }
  13. else {
  14.     die "Improper format. Exiting";
  15. }
  16.  
  17. SWITCH: {
  18.     $mt == "01" && do { $mt = "Jan"; last SWITCH };
  19.     $mt == "02" && do { $mt = "Feb"; last SWITCH };
  20.     $mt == "03" && do { $mt = "Mar"; last SWITCH };
  21.     $mt == "04" && do { $mt = "Apr"; last SWITCH };
  22.     $mt == "05" && do { $mt = "May"; last SWITCH };
  23.     $mt == "06" && do { $mt = "Jun"; last SWITCH };
  24.     $mt == "07" && do { $mt = "Jul"; last SWITCH };
  25.     $mt == "08" && do { $mt = "Aug"; last SWITCH };
  26.     $mt == "09" && do { $mt = "Sep"; last SWITCH };
  27.     $mt == "10" && do { $mt = "Oct"; last SWITCH };
  28.     $mt == "11" && do { $mt = "Nov"; last SWITCH };
  29.     $mt == "12" && do { $mt = "Dec"; last SWITCH };
  30. }
  31.  
  32. print "$ARGV[0] is $dt-$mt-$yr\n";

Code:
[root@hostname test]# ./test.pl 20111231
20111231 is 31-Dec-2011

# 14  
Old 12-15-2011
@ahamed , Your code is giving syntax error.I am using SunOS...match function is not working there.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Date format change from mm/dd/yyyy to yyyymmdd in comma seperate line in perl

Hi All, I have line ,A,FDRM0002,12/21/2017,,0.961751583,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, it contains date in mm/dd/yyyy format i want to change this to yyyymmdd format using perl. Use code tags, thanks. (8 Replies)
Discussion started by: vishal0746
8 Replies

2. Shell Programming and Scripting

Date conversion help from dd/mm/yyyy to dd/Mon/yyyy i.e. 28/10/2012 to 28/Oct/2012

Hi I have a problem with Date format in my code. 1st I am trying to convert today's date to yesterday's using YESTERDAY3=`perl -e '@y=localtime(time()-86400); printf "%04d/%02d/%02d",$y+1900,$y+1,$y;$y;'` And once it is done I am trying to using the yesterday date in a grep command to... (3 Replies)
Discussion started by: nithinankam
3 Replies

3. Shell Programming and Scripting

Converting date DD MM YYYY to DD MON YYYY

Hello, I am writing a script that parses different logs and produces one. In the source files, the date is in DD MM YYYY HH24:MI:SS format. In the output, it should be in DD MON YYY HH24:MI:SS (ie 25 Jan 2010 16:10:10) To extract the dates, I am using shell substrings, i.e.: read line ... (4 Replies)
Discussion started by: Adamm
4 Replies

4. Shell Programming and Scripting

change date format from yyyy/mm/dd to dd/mm/yyyy

(Attention: Green PHP newbie !) I have an online inquiry form, delivering a date in the form yyyy/mm/dd to my feedback form. If the content passes several checks, the form sends an e-mail to me. All works fine. I just would like to receive the date in the form dd/mm/yyyy. I tried with some code,... (6 Replies)
Discussion started by: keyboarder
6 Replies

5. Shell Programming and Scripting

PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy)

Hi All, I am learning PERL for one of the projects, and in one of these scripts, I read a flat text file and print in the terminal. The problem is, the text file has a date field. The format is yyyymmdd. I need to display this as dd-mon-yyyy. Any ideas to do this? Thanks a lot for the... (9 Replies)
Discussion started by: guruparan18
9 Replies

6. 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

7. UNIX for Dummies Questions & Answers

Format date from MM/DD/YYYY to YYYYMMDD

I have a file with some date columns in MM/DD/YYYY format: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|6/6/2007 |MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|11/6/2003|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| And I want to convert the date format to: ... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

8. Shell Programming and Scripting

converting the date field from dd/mm/yyyy to yyyy/mm/dd

How to convert the date field from dd/mm/yyyy to yyyy/mm/dd in unix my script will generate text file which have two fields one is date and another is name of the server for example this is sample date which I have to sort based on older to newer date the problem is when I found out sort will... (4 Replies)
Discussion started by: pareshan
4 Replies

9. Shell Programming and Scripting

Change Date from dd-mmm-yyyy to mm/dd/yyyy

I want to change a date from format dd-mmm-yyyy to mm/dd/yyyy. Is there a way to do this with sed or do you have to write a case statement to convert JAN to 01? Thanks (9 Replies)
Discussion started by: stringzz
9 Replies

10. 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
Login or Register to Ask a Question