![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Code:
echo "YYYYMMDD" | awk '
BEGIN {OFS="/"}
{print substr($1,5,2), substr($1,7,2), substr($1,1,4)}'
Tayyab |
|
#3
|
||||
|
||||
|
Quote:
Using shell script. Code:
#! /bin/ksh
format=YYYYMMDD
YEAR=${format%????}
DAY=${format#??????}
MON=${format#$YEAR}
MON=${MON%$DAY}
echo $MON/$DAY/$YEAR
Code:
echo "YYYYMMDD" | sed -n -e "s_\(....\)\(..\)\(..\)_\2/\3/\1_p" |
|
#4
|
|||
|
|||
|
It worked!
Thank you.
|
|
#5
|
|||
|
|||
|
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
|
||||
|
||||
|
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) Regards, Tayyab Last edited by tayyabq8; 06-13-2006 at 07:54 AM. |
|
#7
|
|||
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |