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
convert date format YYYYMMDD to MM/DD/YYYY nasirgondal Shell Programming and Scripting 8 04-08-2009 10:56 AM
create filename with 'DD/MM/YYYY' date format royalibrahim Shell Programming and Scripting 4 04-12-2008 08:24 AM
Date Vadidation in YYYY-MM-DD itsmehihihi Shell Programming and Scripting 1 07-22-2007 09:22 PM
sed to display date in dd/mm/yyyy format sars Shell Programming and Scripting 2 02-15-2007 03:32 AM
changing YYYY-MM-DD to DDMMYY hcclnoodles Shell Programming and Scripting 3 07-29-2004 07:48 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-27-2008
stringzz stringzz is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 19
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
  #2 (permalink)  
Old 03-27-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
On FreeBSD:
Code:
date -j -f "%d-%b-%Y" "27-Mar-2008" "+%m/%d/%Y"
  #3 (permalink)  
Old 03-27-2008
stringzz stringzz is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 19
Maybe I should have mentioned that I am using korn shell. I get an error when using that.

> date "%d-%b-%Y" "27-Mar-2008" "+%m/%d/%Y"
date: bad conversion
  #4 (permalink)  
Old 03-28-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Next time you should mention your shell and your OS from the beginning
Code:
echo "28-Mar-2008" | date -d - "+%m/%d/%Y"
or..
Code:
#!/bin/ksh
d="28-Mar-2008"
date -d $d "+%m/%d/%Y"
You should take a look over man date anyway.
  #5 (permalink)  
Old 03-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Not all date implementations have all this fancy shmancy stuff. As a matter of fact, on HP-UX or Sun, you should be thankful that it even displays today's date.

If you can't get a good date (sorry, couldn't resist), then yes, sed (or cut, or awk, or perl, or nearly any programming language you happen to know the basics of) can be used to move around parts within the string.

(True story from real life: I had a colleague who needed to remove duplicate lines from a file. I intercepted him when he had exported the file to his University account and loaded it into SPSS which he was more familiar with, but couldn't quite get to do this precise task.)

But yes, you need the month name to number mapping somehow. somebody posted one awk solution the other day, but I can't seem to find it now. If the input is just the date then twelve lines of sed will do it.
  #6 (permalink)  
Old 03-29-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
Quote:
Originally Posted by stringzz View Post
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
Code:
# echo "dd-mmm-yyyy" | awk 'BEGIN{FS="-";OFS="/"}{print $2,$1,$3}'
mmm/dd/yyyy
  #7 (permalink)  
Old 03-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Add to the BEGIN { m[jan] = 1; m[feb] = 2; ... etc } and there you have it. Like I said, somebody posted code like this I think only yesterday, but I wasn't able to bring it up with a search.
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 05:30 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