I have about a thousand PDF files with names like Foo_Jan_2013.pdf, Bar_Feb_2012.pdf, Foo_Mar_2013.pdf, Bar_Mar_2013.pdf. I want to rename these files replacing the month names with numbers, such as Foo_01_2013.pdf, Bar_02_2012.pdf, Foo_03_2013.pdf, Bar_03_2013.pdf.
One approach I tried is to define two arrays (one for the match pattern and other for the replace pattern), then use the respective array members in the search-replace, like so:
match=(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
replace=(01 02 03 04 05 06 07 08 09 10 11 12);
for FILE in *.pdf;
do
for i in {0..11};
do
echo ${match[i]} ${replace[i]}; # Note, see below
echo ${FILE/${match[i]}/${replace[i]}};
done
done
The line marked Note prints Jan 01, Feb 02, etc. as expected, but using them as the search or replace pattern in the next line has no effect, and $FILE gets printed as it is.
I have also confirmed that both of the following work:
${FILE/Jan/01};
foo=Jan;
bar=01;
${FILE/$foo/$bar};
Is it possible to use an array member as the pattern, as I attempted above? If so, what is the correct syntax? If not, what other options do I have to solve this problem?
$BASH_VERSION. Your script works on my Mac running3.2.48(1)-releaseand my Linux running3.2.25(1)-release.