Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A script to 'fix' mp3 file names.....

Status
Not open for further replies.

adamf

Technical User
Mar 7, 2002
51
GB
Just for fun this one....

What I want to be able to do is rename a LIST of mp3 tracks, currently in a format like this:

Album Name - Track Name.mp3

To this:

Album_Name__Track_Name.mp3

So they can be played on a UNIX box.... I'm looking for a script rather than doing it long-hand!

If you want to take it a step further, how would you search for and remove punctuation in the title? Adam F
Solaris System Administrator
 
#--------------------- renameMP3.txt --------------
Album Name 1 - Track Name1.mp3
Album Name 2 - Track Name2.mp3
Album Name 3 - Track Name3.mp3

#--------------------- renameMP3.awk --------------

{
gsub("[ ]*-[ ]*", "__");
gsub("[ ]", "_");
print
}

#---------------------------------------------

awk -f renameMP3.awk renameMP3.txt
 
Good answer ... but I had to use nawk to get it to work. My version of awk doesn't support gsub (Solaris 2.6).

Greg.
 
ooops - I had to do the same ;)

I should really start posting _exactly_ what I do on my Solaris box! ;)

Thanks, grega
 
I managed to get this far. I am getting stuck trying to actually get the routine to do the mv from the old filenames to the new ones the nawk outputs. Adam F
Solaris System Administrator
 
Called as:
renameMP3.ksh renameMP3.txt

#------------------ renameMP3.ksh ---------------------

#!/bin/ksh
fileList="${1}"

while IFS=: read oldFile
do
newFile=$(echo ${oldFile} | nawk -f renameMP3.awk)
cat <<EOF
oldFile - [${oldFile}]
newFile - [${newFile}]
EOF
mv &quot;${oldFile}&quot; ${newFile}
done < ${fileList}
 
What is wrong with the tr command?

Code:
tr ' -' '__' < renameMP3.txt > newlistMP3.txt

This should do just as you want, without all the awk stuff. As UNIX proves, there are multiple methods of feline taxidermy. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
How about using the -s option to squeeze out the extra _'s?

tr -s ' -' '__' < renameMP3.txt > newlistMP3.txt

 
adamf -

Try out

Code:
find . -name &quot;*mp3&quot; |
  awk '
{
  v_from = &quot;\&quot;&quot; $0 &quot;\&quot;&quot;
  gsub( /[      ]*-[    ]*/, &quot;__&quot;, $0 )
  gsub( /  */, &quot;_&quot;, $0 )
  print v_from &quot;, &quot; $0
  system( &quot;mv &quot; v_from &quot; &quot; $0 )
}'

the find part gets the list of files starting at the current directory and the awk command does the rest.

Cheers,
ND [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top