$ echo 'img_11-22-14_XX.jpg' | sed -r 's/[^_]*_([0-9]{2})-([0-9]{2})-([0-9]{2})/newyears_20\3-\1-\2/'
newyears_2014-11-22_XX.jpg
The above looks for anything up to and including the first underline followed by a 6-digit date. It replaces the initial part with newyears_
and reformats the date from mm-dd-yy to 20yy-mm-dd.
The two-digit mm, dd, or yy values are matched with ([0-9]{2})
. The parentheses indicate that sed
should capture the value for later use. The output side of the substitution is _20\3-\1-\2
. This restores the underline and adds a 20
to the front of the year. The year was the third captured value so it is denoted \3
. Likewise, the month was the first captured value so it is denoted \1
and the day the second so it is \2
.
To eliminate some blackslashes, I used the -r
option to invoke extended regular expressions. If you are on a Mac or other non-GNU system, use sed -E
in place of sed -r
. Otherwise, use:
sed 's/[^_]*_\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/newyears_20\3-\1-\2/'