Needed shell script for the following

hi all,
i want the script for the following
my input is date mm-dd-yyyy
i.e 1-4-2012(it doesn't have zeros in the input ,in output it should append zeros and should print the output as defined below)
my output should be 2012-04-01(yyyy-dd-mm)

if my input is 11-14-2012
then my should be 2012-14-11

thanks in advance,
hemanth saikumar

Hi,

Try this one,

echo '1-4-2012' |awk -F'-' '{printf("%04d-%02d-%02d\n", $3, $2, $1);}'

Cheers,
Ranga:)

i want it for variable input.. :frowning: i have to input in command line based on that it should print ..
means if i pass 1-4-2012 then the output should be 2012-04-01
if i pass 11-14-2012 then the output should be 2012-14-11

try:

var=1-4-2012
nvar=$(echo "$var" | awk -F'-' '{printf("%04d-%02d-%02d\n", $3, $2, $1);}')

cant we pass input during executing like

sh filename.sh 1-4-2012 ....

i want to pass it during execution...

thanks for the reply.. :slight_smile:

try in script:

var=$(echo "$1" | awk -F'-' '{printf("%04d-%02d-%02d\n", $3, $2, $1);}')
1 Like

got it but we have to use

echo "$1" | awk -F'-' '{printf("%04d-%02d-%02d\n", $3, $2, $1);}'

thanks for the query dude.. :slight_smile:

If you are using bash or a similar shell, this might be a starting point for your script or function:

ND=${1%-*}
ND="${1##*-}-${ND#*-}-${1%%-*}"
echo $1, $ND

$ ./test "11-14-2012"
11-14-2012, 2012-14-11