[Techtalk] Question: sed s option in a shell script

Conor Daly c.daly at met.ie
Mon Mar 14 21:56:57 EST 2005


On Mon, Mar 14, 2005 at 09:03:20AM +0100 or thereabouts, Almut Behrens wrote:
> On Sun, Mar 13, 2005 at 08:41:30PM -0800, Sabine Konhaeuser wrote:
> > 
> > OLDSUFFIX=wav
> > NEWSUFFIX=mp3
> > for FILE in *."$OLDSUFFIX"
> > do
> >      NEWNAME=´"$FILE" | sed -e "s/$OLDSUFFIX/$NEWSUFFIX/"´
> >      lame --preset standard "$FILE" "$NEWNAME"
> > done
> 
> You're close to the solution...

And then, once you've got the solution (so you know sed works), there's
always more than one way to do it...  I'd use 'basename' here instead.
basename has syntax like:

basename "path/to/file.extension" [extension]

and returns "file.extension" or "file." if you include the 'extension' bit.
So: your filename.wav can be converted to filename.mp3 with:

NEWNAME=`basename $FILE $OLDSUFFIX`mp3

This does: "Take the filename of $FILE apart from the trailing $OLDSUFFIX
bit and append the string 'mp3' to it"

If you have directories included as part of the filename, you'd need to use
'dirname' to extract the path as well.

NEWNAME=`dirname $FILE'`basename $FILE $OLDSUFFIX`mp3

Conor
-- 
Conor Daly,                   Please avoid sending me 
Met Eireann, Glasnevin Hill,  Word or PowerPoint attachments.
Dublin 9, Ireland             http://www.fsf.org/philosophy/no-word-attachments.html
Ph +3531 8064276 Fax +3531 8064247


**********************************************************************
This e-mail and any files transmitted with it are confidential 
and intended solely for the addressee. If you have received
this email in error please notify the sender.
This e-mail message has also been scanned for the
presence of computer viruses.
**********************************************************************



More information about the Techtalk mailing list