[Gllug] perl script
Tethys
tet at accucard.com
Thu Aug 22 08:53:54 UTC 2002
>opendir (DIR, '.');
>foreach ( grep {/\.jpeg$/} readdir DIR ) {
> /^(.)(.)(.)(.)(.)(.*)$/;
> rename ($_, "$4$5$3$1$2$6");
>}
Proof positive once again that perl should be avoided wherever possible!
Compare the readability of that with:
#!/bin/sh
for i in *.jpeg
do
n1=`echo "$i" | cut -c1-2`
n2=`echo "$i" | cut -c3`
n3=`echo "$i" | cut -c4-5`
n4=`echo "$i" | cut -c6-`
newname="${n3}${n2}${n1}${n4}"
mv "$i" "$newname"
done
It's not the most efficient way of doing things, but unless you're
intending to process tens of thousands of files, it doesn't *need*
to be. Remember, premature optimization is the root of all evil.
Mine has the advantage of being understandable by anyone with basic
scripting knowledge. If you're really worried about the overhead
needed to fork cut, then it can all be done natively in bash, at
the expense of portability to platforms without bash:
n1=${i:0:2}
n2=${i:2:1}
n3=${i:3:2}
n4=${i:5}
FYI, if you do want to use a regexp (in perl, sed, or whatever),
then you don't need to do each character individually:
sed 's/\(..\)\(.\)\(..\)\(.*\)/\3\2\1\4/'
Tet
--
Gllug mailing list - Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug
More information about the GLLUG
mailing list