You are not logged in.

#1 2010-04-21 20:33:55

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Replace special characters

Hi,
I want to rename files containg special characters in a directory tree.

For example I want

06\ --\ Doppelgänger.flac

renamed to

06\ --\ Doppelganger.flac

I have found a python script that seems to be able to do it here.

I have also found a list of replacements characters

my $AccentedChars  = '€–‚ƒ„…†‡ˆ‰Š‹Œ–Ž––''""•–—˜™š›œ–žŸ'.
                     '–¡¢£¤¥¦§¨©ª«¬-®¯°±²³´µ¶·¸¹º»¼½¾¿'.
                     'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞß'.
                     'àáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ';

my $NonAccenChars  = 'E-,f,.++^%S_O-Z--````.--~Ts_o-zY'.
                     '-!cLoYlS`Ca_--R-`+23`uP.,10_qh3_'.
                     'AAAAAAACEEEEIIIIDNOOOOOxOUUUUYTS'.
                     'aaaaaaaceeeeiiiidnooooo%ouuuuyty';

from this perl script (which throws an error when used. Otherwise it would be perfect).

So, can somebody tell me how I could feed the python script with the above replacement table?
I have tried but I can't seem to get it to work.
I have also tried to make a simple bash-script using rename but I could not that to work either.

I would really appreciate any help.

Thanks,
Rasmus


Arch x64 on Thinkpad X200s/W530

Offline

#2 2010-04-21 21:25:30

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Replace special characters

I didn't try the perl script, but sed's y command will do exactly this. tr complains about a dash

accent='€–‚ƒ„…†‡ˆ‰Š‹Œ–Ž––''""•–—˜™š›œ–žŸ–¡¢£¤¥¦§¨©ª«¬-®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
nonaccent='E-,f,.++^%S_O-Z--````.--~Ts_o-zY-!cLoYlS`Ca_--R-`+23`uP.,10_qh3_AAAAAAACEEEEIIIIDNOOOOOxOUUUUYTSaaaaaaaceeeeiiiidnooooo%ouuuuyty'
echo '06\ --\ Doppelgänger.flac' | sed "y/$accent/$nonaccent/"

So you could do something like

for file in *; do
mv -v -i -- "$file" "$(echo "$file" | sed "y/$accent/$nonaccent/")"
done

Offline

#3 2010-04-22 02:39:30

Pank
Member
From: IT
Registered: 2009-06-13
Posts: 371

Re: Replace special characters

Hi,
Thanks. That is a nice simple solution. I will try it out tonight.
Can one easily make it recursive? My files are stored as

Artist\Album\*.flac

I am guessing one could just say

for file in */*/*; do
...

Also, does one need to define $file to only be files which actually contains special characters?

Thanks,
Rasmus


Arch x64 on Thinkpad X200s/W530

Offline

#4 2010-04-22 07:25:14

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Replace special characters

Yeah, you could even do for file in */*/*.flac
Or

shopt -s globstar
for file in **/*.flac; do ...

mv won't mess up the file if it doesn't contain special characters, like mv a a. You could add >2/dev/null after the command to hide the messages and see the actual work.

Offline

Board footer

Powered by FluxBB