[Techtalk] Question: sed s option in a shell script
Devdas Bhagat
devdas at dvb.homelinux.org
Mon Mar 14 18:38:00 EST 2005
On 13/03/05 20:41 -0800, Sabine Konhaeuser wrote:
> Hi Everyone,
>
> I'm in the process of digitizing my record collection. I will save the
> recorded songs as flac and mp3 files. The flac command flac -8 *.wav
> loops through the entire directory and creates the flac files. So far
> so good. Lame however doesn't seem to do this. So I wrote a small
> script that loops through the directory, fetching every wav file and
> then with sed replace the ending wav with mp3. This is the script:
>
> **** script starts here ****
>
> OLDSUFFIX=wav
> NEWSUFFIX=mp3
> for FILE in *."$OLDSUFFIX"
> do
> NEWNAME=´"$FILE" | sed -e "s/$OLDSUFFIX/$NEWSUFFIX/"´
> lame --preset standard "$FILE" "$NEWNAME"
> done
>
> **** script ends here ****
>
#!/bin/bash
#This should work wiht /bin/sh but I haven't tested it with that
OLDSUFFIX=wav
NEWSUFFIX=mp3
for FILE in *.${OLDSUFFIX}
do
# Run the command in backticks to get the desired output.
# sed -e takes double quotes intead of single quotes because we want the
# shell to interpret the variables.
# The $ at the end of the regular expression forces a change only a the
# end. Eg: A file named waves.wav
NEWNAME=`echo ${FILE}|sed -e "s/${OLDSUFFIX}$/${NEWSUFFIX}/"`
lame --preset standard ${FILE} ${NEWNAME}
done
<snip>
> The script itself does work, it loops through all the wav files and
> creates the mp3 files. But the file name is not file_name.mp3 but
> file_name.wav.mp3. Not that this is a big deal, but the wav in the
> name bothers me and I'd like to have it removed automatically. Right
> now I rename the files by hand.
>
There is also rename(1) for bulk renaming.
Devdas Bhagat
More information about the Techtalk
mailing list