Unix Shell - Re: Substituing groups elements in file names & THANK YOU A LOT!

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2005 > Re: Substituing groups elements in file names & THANK YOU A LOT!





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Substituing groups elements in file names & THANK YOU A LOT!
tacoguy

2005-04-27, 8:51 pm

tacoguy wrote:
> I have a very large number of files named in the format:
>
> 1946-March-23, history.jpg
>
> I need them to be named in ANSI format:
>
> 1946-03-23, history.jpg
>
> In other words I want to substitute the group (Januray, February...)
> with the group (01,02) within file names.


I tested it, it works very fine, her is what I use from now on:

for FILE in *-*-*.jpg ## To avoid adding extra - at
the end of filename if none is already present
do
oldIFS=$IFS
IFS=-
case $FILE in ## To avoid truncating after a
third -
*-*-*-*)
one=${FILE%%-*}
temp=${FILE#"$one"-}
two=${temp%%-*}
three=${temp#"$two"-}
set -- "$one" "$two" "$three"
;;
*-*-*)
oldIFS=$IFS
IFS=-
set -- $FILE
;;
esac
month=$2
case $month in ## Creation of the string
that will replace the filename
January) month=01 ;;
February) month=02 ;;
March) month=03 ;;
April) month=04 ;;
May) month=05 ;;
June) month=06 ;;
July) month=07 ;;
August) month=08 ;;
September) month=09 ;;
October) month=10 ;;
November) month=11 ;;
December) month=12 ;;
esac
NEWFILE=$1-$month-$3
mv "$FILE" "$NEWFILE" ## Substitution of the old filename
with the new one
IFS=$oldifs
done
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com