Help with bash command to change lots of file names

Frank Shute Frank at
Fri May 10 02:17:49 BST 2002

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.



 Tel: 01423 323019
PGP keyID: 0xC0B341A3

Don't change the reason, just change the excuses!
		-- Joe Cointment

More information about the Ukfreebsd mailing list