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
create filename with 'DD/MM/YYYY' date format royalibrahim Shell Programming and Scripting 4 04-12-2008 08:24 AM
sed to display date in dd/mm/yyyy format sars Shell Programming and Scripting 2 02-15-2007 03:32 AM
How can i get the yesterday's date in YYYYMMDD format prasadsr HP-UX 4 01-19-2007 09:52 AM
convert mmddyy date format to ccyyddd format?? Bhups Shell Programming and Scripting 2 09-27-2006 11:30 PM
get yesterday date in yyyymmdd format hk_newbie UNIX for Dummies Questions & Answers 2 12-14-2001 03:32 PM

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 06-07-2006
nasirgondal nasirgondal is offline
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.
  #2 (permalink)  
Old 06-08-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Code:
echo "YYYYMMDD" | awk '
BEGIN {OFS="/"}
{print substr($1,5,2), substr($1,7,2), substr($1,1,4)}'
Regards,
Tayyab
  #3 (permalink)  
Old 06-08-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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"
  #4 (permalink)  
Old 06-08-2006
nasirgondal nasirgondal is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 4
It worked!

Thank you.
  #5 (permalink)  
Old 06-13-2006
arunkumar_mca arunkumar_mca is offline
Registered User
  
 

Join Date: Oct 2004
Posts: 255
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....
  #6 (permalink)  
Old 06-13-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
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 10:54 AM..
  #7 (permalink)  
Old 08-07-2008
rspk_praveen rspk_praveen is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 5
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
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 04:42 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