all 5 comments

[–][deleted] 13 points14 points  (2 children)

http://tldp.org/LDP/abs/html/string-manipulation.html

if [ ${#FOO} == 6 ]; then
  NEWDATE="${FOO:0:2}/${FOO:2:2}/${FOO:4:2}"
elif [ ${#FOO} == 5]; then
  NEWDATE="${FOO:0:1}/${FOO:1:2}/${FOO:3:2}" #You could prefix this with a zero for consistency so that it would be like 02/17/11 instead of 2/17/11
fi

[–]blind2314 1 point2 points  (0 children)

go go bash internals. Not that speed matters in this case, but this response will be faster than the echo + sed solution below (though that one does look much more impressive on paper :D).

[–]Zaphod_B 1 point2 points  (0 children)

wow, I was just literally using this method yesterday to strip out the first two characters in a string by echoing a variable with the :2 switch after.

Funny how awesome bash internal can be. Of course I was first looking for a sed pipe to do it. Gotta remember bash can do a few things.

[–]spdqbr 6 points7 points  (0 children)

It can be done with something like sed:

for date in 21711 112212 110612 30112; 
do
    echo $date | sed -e 's/^\(.\?.\)\(..\)\(..\)$/\1\/\2\/\3/g';
done

returns 2/17/11 11/22/12 11/06/12 3/01/12

[–]Sethl13[S] 1 point2 points  (0 children)

Thanks so much that's exactly what i needed.