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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
create filename with 'DD/MM/YYYY' date format royalibrahim Shell Programming and Scripting 4 04-12-2008 05:24 AM
sed to display date in dd/mm/yyyy format sars Shell Programming and Scripting 2 02-15-2007 12:32 AM
How can i get the yesterday's date in YYYYMMDD format prasadsr HP-UX 4 01-19-2007 06:52 AM
convert mmddyy date format to ccyyddd format?? Bhups Shell Programming and Scripting 2 09-27-2006 08:30 PM
get yesterday date in yyyymmdd format hk_newbie UNIX for Dummies Questions & Answers 2 12-14-2001 12:32 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-07-2006
Registered User
 

Join Date: Jun 2006
Posts: 4
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.
Reply With Quote
Forum Sponsor
  #2  
Old 06-07-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
Code:
echo "YYYYMMDD" | awk '
BEGIN {OFS="/"}
{print substr($1,5,2), substr($1,7,2), substr($1,1,4)}'
Regards,
Tayyab
Reply With Quote
  #3  
Old 06-07-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Quote:
Originally Posted by nasirgondal
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.
You have broken a rule by duplicating your thread. The other one is here - changing the format of date.

Using shell script.
Code:
#! /bin/ksh

format=YYYYMMDD

YEAR=${format%????}
DAY=${format#??????}
MON=${format#$YEAR}
MON=${MON%$DAY}
echo $MON/$DAY/$YEAR
Using sed.
Code:
echo "YYYYMMDD" | sed -n -e "s_\(....\)\(..\)\(..\)_\2/\3/\1_p"
Reply With Quote
  #4  
Old 06-08-2006
Registered User
 

Join Date: Jun 2006
Posts: 4
It worked!

Thank you.
Reply With Quote
  #5  
Old 06-13-2006
Registered User
 

Join Date: Oct 2004
Posts: 235
hi all....

Want some clarification ..

In the below scripts ...

echo "YYYYMMDD" | awk 'BEGIN {OFS="/"}{print substr($1,5,2), substr($1,7,2), substr($1,1,4)}'

What is OFS..Also how does the '/' is placed inbetween..

Thanks in advance,
Arun....
Reply With Quote
  #6  
Old 06-13-2006
tayyabq8's Avatar
Moderator
 

Join Date: Nov 2004
Location: Bahrain
Posts: 555
OFS is Output Field Separator, I don't know the internal technicle details of awk though I can imagine that awk puts each print command in a stack like
Code:
print substr($1,5,2)
print substr($1,7,2)
print substr($1,1,4)
and then separates each output of print with an OFS if defined, default OFS is " " a whitespace. Hope it helps.

Regards,
Tayyab

Last edited by tayyabq8; 06-13-2006 at 07:54 AM.
Reply With Quote
  #7  
Old 08-07-2008
Registered User
 

Join Date: Aug 2008
Posts: 3
hi nasirgondal,

I also wanted such a script. It works fine but if the date is not exactly in YYYYMMDD format, lets say YYYYMDD or YYYYMMD format its not working. Is there any solution even for this. Appreciate your help

#! /bin/ksh

format=YYYYMMDD

YEAR=${format%????}
DAY=${format#??????}
MON=${format#$YEAR}
MON=${MON%$DAY}
echo $MON/$DAY/$YEAR
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0