Command needed

Hi,

I have the date as 20130101 and i need it to rephrased to 2013-01-01.

Any command which can do this.

$ date -d"20130101" +"%Y-%m-%d" 
2013-01-01

$ date --date="20130101" +"%Y-%m-%d"
2013-01-01

go through man date

--edit--

if you don't know where to post thread post here
UNIX Desktop for Dummies Questions & Answers - The UNIX and Linux Forums

mydate='20130301'
mydate=`echo "${mydate}" | sed 's,\(....\)\(..\)\(..\),\1-\2-\3,'`
sed 's/./&-/6; s//&-/4' 

--
Note: date --date or date -d is GNU date only...

Just so many methods to choose from... ;o)
Longhand...

Last login: Wed Jan 29 19:19:26 on ttys000
AMIGA:barrywalker~> mydate="20130101"
AMIGA:barrywalker~> echo "${mydate:0:4}-${mydate:4:2}-${mydate:6:2}"
2013-01-01
AMIGA:barrywalker~> _
1 Like

The offering from wisecracker is probably easiest to understand and reuse for other purposes, but be aware that this is ksh93 or bash only. Older ksh probably won't support it.

The date functions are great if they work, but I haven't seen a good document on them yet. My man page for date is woeful. RHEL 6.3 even has:-

To use an older ksh, try:-

mydate="20130101"
my_yyyy="${mydate%????}"
my_dd="${mydate#??????}"
my_mm="${mydate%$my_dd}"
my_mm="${my_mm#????}"

echo "${my_yyyy}-${my_mm}-${my_dd}"

I hope that this helps, if you need it.

Robin

1 Like