Shell script for Date conversion from mm/dd/yy to mm/dd/yyyy

Seek help in coverting date from mm_dd_yy to mm_dd_yyyy.
I get a date in a file some thing file like this
09_14_11.pdf
Need to convert the same to
09_14_2011.pdf

:wall:

$ echo "09_14_11.pdf" | ruby -e 'print gets.sub(/_11/,"_2011")'
09_14_2011.pdf

There are many ways to do that, but what have you tried?

Date changes randomly.. So if you can help in making this generalized command it will help.
09_14_11.pdf
03_04_04.pdf
05_04_07.pdf

I am new to unix and shell scrpt. Your timely help is greatly appreaciated.

$ echo "09_14_11.pdf" | ruby -e 'print gets.sub(/_(\d+)\./,"_20\\1.")'
09_14_2011.pdf
$ echo "03_04_04.pdf" | ruby -e 'print gets.sub(/_(\d+)\./,"_20\\1.")'
03_04_2004.pdf

Thanks for your quick response.
I am on AIX... It says ksh: ruby: not found

Can you please help

Not sure if you need to deal with 1900 dates too:

$ echo "12_23_92.pdf
01_01_08.txt
01_10_60.txt" | 
sed -e 's/\([0-9][0-9]\)_\([0-9][0-9]\)_\([3-9][0-9]\)\./\1_\2_19\3./' -e 's/\([0-9][0-9]\)_\([0-9][0-9]\)_\([0-2][0-9]\)\./\1_\2_20\3./'
12_23_1992.pdf
01_01_2008.txt
01_10_1960.txt

Otherwise:

sed 's/\([0-9][0-9]\)_\([0-9][0-9]\)_\([0-2][0-9]\)\./\1_\2_20\3./'

Thanks a bunch...

I tried this one.. can u please validate.

string="040630"
year=`echo $string | cut -c 1-2`
month=`echo $string | cut -c 3-4`
day=`echo $string | cut -c 5-6`
year=`expr $year + 2000`

I amnot sure if all the scenarios will be covered here or not.

That looks fine - purests may say that the use of external commands (like cut and expr) should be avoided as there is a performance hit in loading the command and the shell is powerfull enough to do the same task.

However if your not processing hundreds of strings its hardly worth the loss in portability and readability.

Here is an alternative way using awk and sed - changing ALL the *.pdf files made up in a similar way (double digit _ double digit _ double digit.pdf) in all the subdirectories from your current one down and replacing them the way you wanted it.

ls
09_14_11.pdf
find . -name "*.pdf" | awk '{print("mv "$1" "$1)}' | sed 's/\([0-9]\{1,2\}\)_\([0-9]\{1,2\}\)_\([0-9]\{1,2\}\)/\1_\2_20\3/2' | /bin/ksh
ls
09_14_2011.pdf