LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-20-2010, 11:00 AM   #1
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Rep: Reputation: 0
Renaming MP3 collection [bash script]


Hello everybody,

I've been trying (and trying...) to write this script, and now, i am at i point where i cannot go any further without any help from some (more experienced) users here
I'm a complete newbie on bash scripting, so be gentle

This is what i've come up so far:
Code:
./reading 
.---------------------------------- - - -- -- -- --- - - - - - - - - - -
| Number of characters 48
| + Current: 107_david_guetta_-_missing_you_(feat._novel).mp3
| + Changed: 107_david_guetta-missing_you_(ft._novel).mp3
| + Matched: "(feat._" substitute "(ft._"
| + Morping: "107_david_guetta-missing_you_(feat._novel).mp3" to "107_david_guetta-missing_you_(ft._novel).mp3"
'-[END]---------------------------- - - -- -- -- --- - - - - - - - - - -
| Number of characters 69
| + Current: 108_david_guetta_-_choose_(feat._ne-yo_and_kelly_rowland)_(radio).mp3
| + Changed: 108_david_guetta-choose_(ft._ne-yo_and_kelly_rowland)_(radio).mp3
| + Matched: "(feat._" substitute "(ft._"
| + Morping: "108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio).mp3" to "108_david_guetta-choose_(ft._ne-yo_and_kelly_rowland)_(radio).mp3"
'-[END]---------------------------- - - -- -- -- --- - - - - - - - - - -
| + Changed: 108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio_edit).mp3
| + Matched: "(radio)" substitute "(radio_edit)"
| + Morping: "108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio).mp3" to "108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio_edit).mp3"
mv: cannot stat `108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio).mp3': No such file or directory
'-[END]---------------------------- - - -- -- -- --- - - - - - - - - - -
The problem is that it works with 1 variable to change, but when it uses 2 or more i'm getting errors (as stated above)

This is the code
It loads the variables from rsub file.
Code:
#!/bin/sh
numbers=*-*  # * (wild card) matches everything between.
bpm=\(*bpm\).mp3
removenumeric=${mp3#$numbers}
removebpm=${mp3%%$bpm}.mp3
echo \.---------------------------------- - - -- -- -- ---  -   - -    -   -    - - -    -    -
for mp3 in ls *.mp3; do
if [ -e "$mp3" ]
then
echo \| "Number of characters ${#mp3}"
echo \| \+ Current: ${mp3} 


#replacing spaces with underscores, changing to lowercase
removed="$(echo ${mp3} | sed 's/ /_/g' | sed 's/_-_/-/g' | tr '[A-Z]' '[a-z]')"
if [ "$mp3" != "$removed" ] ; then
mv $mp3 $removed

  while read inputline
  do
  match="$(echo $inputline | cut -d: -f1)"
  substitute="$(echo $inputline | cut -d: -f2)"
  corrected=$(echo $removed | sed 's/'$match'/''\'$substitute'/g')
  	if [ "$removed" != "$corrected" ] ; then
	echo \| \+ Changed: $corrected
	echo \| \+ Matched: \"$match\" substitute \"$substitute\"
	echo \| \+ Morping: \"$removed\" to \"$corrected\"
	mv $removed "$(echo $removed | sed 's/'$match'/''\'$substitute'/g')"
	echo \'-[END]---------------------------- - - -- -- -- ---  -   - -    -   -    - - -    -    -
	fi
  done < rsub

else echo Nothing to do!
fi
fi
done

Last edited by mainstream; 12-21-2010 at 09:16 AM.
 
Old 12-20-2010, 05:22 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
I'd probably separate them out into unique operations which will make it easier to extend and troubleshoot, coming up with a miracle one-liner is great for fun but sometimes not that maintainable.

eg.
Code:
# remove leading numbers
...
# spaces to underscores
...
# tolower
...
# feat to ft.
...
# versus to vs.
...
cheers
 
Old 12-21-2010, 04:45 AM   #3
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Ok thanks for your reply.

I've already included this:
Code:
while read inputline
do
  match="$(echo $inputline | cut -d: -f1)"
  substitute="$(echo $inputline | cut -d: -f2)"
  corrected=$(echo $file | sed 's/'$match'/'$substitute'/g')
  	if [ "$file" != "$corrected" ] ; then
	echo match \"$match\" substitute \"$substitute\"
	echo Match found "renaming \"$file\" to $corrected"
	local file=$corrected
	fi
done < rsub
exit 0
I already made this script, and tried it. It works very well.
But how can i make it this 2 loops works together?

Regards

Last edited by mainstream; 12-21-2010 at 06:43 AM.
 
Old 12-21-2010, 05:55 AM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Double quotes should be fine
 
Old 12-21-2010, 09:16 AM   #5
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
I've changed the first post :-) hope you can help me with this!
ANd thanks for your time ^^
 
Old 12-21-2010, 03:52 PM   #6
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
<winge>It's a little hard to follow if you go back and edit your original posts .. linear is better. </winge>

Quote:
| Number of characters 69
| + Current: 108_david_guetta_-_choose_(feat._ne-yo_and_kelly_rowland)_(radio).mp3
| + Changed: 108_david_guetta-choose_(ft._ne-yo_and_kelly_rowland)_(radio).mp3
| + Matched: "(feat._" substitute "(ft._"
| + Morping: "108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio).mp3" to "108_david_guetta-choose_(ft._ne-yo_and_kelly_rowland)_(radio).mp3"
'-[END]---------------------------- - - -- -- -- --- - - - - - - - - - -
| + Changed: 108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio_edit).mp3
| + Matched: "(radio)" substitute "(radio_edit)"
| + Morping: "108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio).mp3" to "108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio_edit).mp3"
mv: cannot stat `108_david_guetta-choose_(feat._ne-yo_and_kelly_rowland)_(radio).mp3': No such file or directory
'-[END]---------------------------- - - -- -- -- --- - - - - - - - - -
The problem is that you're still trying to use the same file name variable which no longer exists because it's been renamed due to the previous "feat. to ft."

This is why I suggested you run them as discrete operations

cheers
 
Old 12-22-2010, 06:20 AM   #7
mainstream
Member
 
Registered: Oct 2010
Location: localhost
Distribution: Ubuntu / Linux Mint
Posts: 61

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by kbp View Post
<winge>It's a little hard to follow if you go back and edit your original posts .. linear is better. </winge>



The problem is that you're still trying to use the same file name variable which no longer exists because it's been renamed due to the previous "feat. to ft."

This is why I suggested you run them as discrete operations

cheers
Ahh yes i figured out, i figured ;-) thanks
Now i used to mostly used as discrete operations :-)

For other things i use:
If i just use
Code:
 rename "'s/${match}/${substitute}/g'" *.mp3
it works.
Except for filenames/substitutes which need escape characters i still need to find out.
Code:
mainstream@serotonin-pc:/media/500GB/music$ rename 's/(radio)/(radio_edit)/g' ghostface_killah-back_like_that_(remix)_(ft._kanye_west_and_ne-yo)_(radio).mp3
bash: syntax error near unexpected token `(' (output from script)
mainstream@serotonin-pc:/media/500GB/music$ rename 's/\(radio\)/\(radio_edit\)/g' ghostface_killah-back_like_that_\(remix\)_\(ft._kanye_west_and_ne-yo\)_\(radio\).mp3 (this is how i want it)
do you what the best way is?

Thanks again!
 
Old 12-22-2010, 06:29 PM   #8
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Try single quotes around the file name
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script for sorting and renaming multiple mp3 files by id3 tags simonloach Linux - General 8 02-16-2013 09:07 AM
Bash script for renaming files (all odd) snaggletooth1134 Programming 10 05-01-2012 12:26 AM
Bash-Script-Collection? Cornholio Programming 2 12-25-2004 06:21 AM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM
mp3 name 'squeezer' bash script ?? purpleburple Linux - General 7 12-12-2002 04:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration