[Gllug] perl script

Thom May thom at positive-internet.com
Wed Aug 21 18:42:47 UTC 2002


* Nick Mailer (nickm at positive-internet.com) wrote :
> On Wednesday 21 August 2002 6:09 pm, Philippe Rousselot wrote:
> > Hi,
> >
> > I need to modify the title of several files and would like to run a script
> > I need to invert letter 1 with letter 4 and letter 2 with letter 5
> >
> > 01_02_03_04.jpeg becomes 02_01_03_04.jpeg
> >
> >
> > #!/bin/bash
> > for i in *jpeg
> > do
> > rename ???????
> > done
> >
> >
> >
> > Your help would be greatly appreciated.
> >
> 
> This Perl I've knocked up (cos I don't like shell) should work (presuming you 
> start in the CWD, otherwise make the path explicit in the rename/opendir):
> 
> 
> opendir (DIR, '.');
> foreach ( grep {/\.jpeg$/} readdir DIR ) {
>     /^(.)(.)(.)(.)(.)(.*)$/;
>     rename ($_, "$4$5$3$1$2$6");
> }
> 
> 
Ber. forking perl for this is uber-overkill
#!/bin/bash
for i in *jpeg
do
        new=$(echo $i|sed -e \
        's#^\(.\)\(.\)\(.\)\(.\)\(.\)\(.*\)#\4\5\3\1\2\6#');
        mv $i $new;
done

Sorry about the nasty sed regexp - it's doable with bash direct but that is
dark magic.
-Thom
-- 
The Positive Internet Company

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list