1

@terdon's answer provides this script:

find . -type f -name "*mp3" | while read file; do 
    album="$(basename "$(dirname "$file")")"; 
    filename="$(basename "$file")"; 
    artist=${filename%%-*}; 
    title=${filename##*-}; 
    title=${title%%.mp3}; 
    eyeD3 -A "$album" -t "$title" -a "$artist" "$file"; 
done

It makes use of eyeD3 to update mp3 tags. It extracts Album, Artist and Title from file name, supposing that the file name is Album/Artist - Title.mp3.

Well, after running it the Title field is starting with a blank space. See below:

enter image description here

How to fix this if my mp3 files have the pattern

Album/<Artist with possible spaces> - <track number> - <Title with possible spaces>.mp3
4
  • This might not be in the domain of bash scripting anymore. Using python instead might be beneficial.
    – Alexander
    Feb 9, 2014 at 19:29
  • possible duplicate of MP3 tags Cyrillic chars
    – Mikel
    Feb 9, 2014 at 21:48
  • @Sigur I don't understand why you didn't just update your question and comment there.
    – Mikel
    Feb 9, 2014 at 21:49
  • It was suggested to me to ask as a new question since the problem now is related with script and strings manipulation. Not necessarily tags.
    – Sigur
    Feb 9, 2014 at 21:59

3 Answers 3

1

Since I assume that your names do not always have these spaces, the easiest thing to do would be to simply remove the space if present:

find . -type f -name "*mp3" | 
  while read file; do 
   album="$(basename "$(dirname "$file")")"; 
   filename="$(basename "$file")"; 
   artist=${filename%%-*}; artist=${artist%% }; artist=${artist## };
   title=${filename##*-}; title=${title%% }; title=${title## };
   title=${title%%.mp3}; 
   eyeD3 -A "$album" -t "$title" -a "$artist" "$file";
 done
1
  • Perfect. Now everything is fine. Thanks so much.
    – Sigur
    Feb 9, 2014 at 20:04
1

Ksh extended glob patterns give wildcards the power of regular expressions. In bash, turn them on with shopt -s extglob.

Then, instead of

title=${filename##*-}

to remove the part of $filename up to the last -, you can use

title=${filename##*- #}

to also strip whitespace after the -.

0

If you just want to remove the leading space for the Artist, you could use sed. Something like this should work.

artist=${filename%%-*}; 
artist=`echo $artist |  sed 's/^ *//g'`;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.