| Chris F.A. Johnson 2005-04-28, 5:59 pm |
| On Thu, 28 Apr 2005 at 14:35 GMT, Ed Morton wrote:
>
>
> tacoguy wrote:
>
><snip>
>
> Note that should be IFS="$oldIFS".
>
>
> Y'know, you don't actually need to type out all the months. For example,
> in ksh with GNU date:
>
> PS1> date +"%Y-%m-%d" -d "23-March-1946"
> 1946-03-23
>
> I also don't think you need to worry about the potential extra "-"s in
> the file name since all you really need to do is split your file name at
> the comma, then shuffle the "-"-separated fields that were in the date
> part, e.g.:
>
> PS1> FILE="1946-March-23, history.jpg"
> PS1> date="${FILE%%,*}"
> PS1> rest="${FILE#*,}"
> PS1> IFS=-
> PS1> set -- $date
> PS1> NEWFILE=`date +"%Y-%m-%d" -d "$3-$2-$1"`",$rest"
> PS1> echo "\"$FILE\" -> \"$NEWFILE\""
> "1946-March-23, history.jpg" -> "1946-03-23, history.jpg"
>
> so I think all you really need your script to be is:
>
> for FILE in *-*-*.jpg
> do
> oldIFS="$IFS"
> date="${FILE%%,*}"
> rest="${FILE#*,}"
> set -- $date
> NEWFILE=`date +"%Y-%m-%d" -d "$3-$2-$1"`",$rest"
> mv "$FILE" "$NEWFILE"
> IFS="$oldIFS"
> done
It only works with GNU date. The case statement to generate the
month number is more portable and faster. I usually have it in a
function:
_monthnum()
{
case ${1#0} in
1|[Jj][aA][nN]*) _MONTHNUM=01 ;;
2|[Ff][Ee][Bb]*) _MONTHNUM=02 ;;
3|[Mm][Aa][Rr]*) _MONTHNUM=03 ;;
4|[Aa][Pp][Rr]*) _MONTHNUM=04 ;;
5|[Mm][Aa][Yy]*) _MONTHNUM=05 ;;
6|[Jj][Uu][Nn]*) _MONTHNUM=06 ;;
7|[Jj][Uu][Ll]*) _MONTHNUM=07 ;;
8|[Aa][Uu][Gg]*) _MONTHNUM=08 ;;
9|[Ss][Ee][Pp]*) _MONTHNUM=09 ;;
10|[Oo][Cc][Tt]*) _MONTHNUM=10 ;;
11|[Nn][Oo][Vv]*) _MONTHNUM=11 ;;
12|[Dd][Ee][Cc]*) _MONTHNUM=12 ;;
*) return 5 ;;
esac
}
Then the new filename can be generated with:
_monthnum $2
NEWFILE=$1-$_MONTHNUM-$3
--
Chris F.A. Johnson <http://cfaj.freeshell.org>
========================================
==========================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/ssr.html>
|