Help with bash command to change lots of file names

Frank Shute Frank at esperance-linux.co.uk
Fri May 10 14:24:52 BST 2002


On Fri, May 10, 2002 at 02:17:49AM +0100, I wrote:
>
> On Thu, May 09, 2002 at 11:25:42PM +0100, greadey wrote:
> >
> >     Hi peeps,
> > 
> > I have a directory with lots of files called CHAPN.TXT where n is a
> > number from 1 to 14.  I want to turn them all into latex files and so
> > for ease of editing in Vim I want to change them all to chN.tex.  I have
> > been trying to write a bash command to do it with a for loop and sed and
> > I basically have;
> > 
> > for f in *
> > do
> > NEW=`echo $f | sed -e 's/CHAP \( \W+ \. \) /ch \1 tex/ ' ` # I've put
> > whitespace in for clarity
> > #mv $f $NEW
> > echo $NEW
> > done
> > 
> > I have tried different quotes and I believe my regexp is correct but
> > when I run this command or any variation I either get an error if I use
> > all single quotes instead of backticks and single quotes or the command
> > just prints all the original file names.
> > 
> > Can someone tell me where I am going wrong?
> > 
> > Thanks in advance
> > 
> > greadey.
> 
> I'm not wildly keen on your regex & I prefer brackets to backticks
> (more legible!). Try:
> 
> NEW=$(echo $f | sed -e 's/CHAP\([1-9]*\)\.TXT/ch\1\.tex/')
> 
> Works OK with /bin/sh (I think...) don't know about bash.

I fouled up of course and I'd probably write the whole thing as:

TARGET=$(ls -1 | grep 'CHAP[0-9]*.TXT')
for filename in $TARGET
do
NEW=$(echo $filename | sed -e 's/foo\([0-9]*\)\.txt/bogus\1\.tex/')
#mv $filename $NEW
echo $NEW
done

Probably wrong again;) and if I had a choice I'd use perl rather than
shell.


-- 

 Frank 

*-*-*-*-*-*-*-*-*-*-*
   Boroughbridge.
 Tel: 01423 323019
     ---------
PGP keyID: 0xC0B341A3
*-*-*-*-*-*-*-*-*-*-*

http://www.esperance-linux.co.uk/


The last person who said that (God rest his soul) lived to regret it.




More information about the Ukfreebsd mailing list