tags:

views:

42

answers:

1

How do I write a bash shell script (under Mac OS X) to convert the names of various files with special characters such as ä, ö, ü? Using sed doesn't work:

echo * | sed s/ü/ue/

Can I do it some other way?

EDIT

Here is the full script. It basically zips up iPhone app packages and then (should) convert any special characters in the zip file name:

#/bin/bash

for appfile in *.app
do
    zipfile=`echo ${appfile} | sed s/app/zip/`
    zipfile=`echo ${zipfile} | sed s/\ /_/`
    # these don't work...
    zipfile=`echo ${zipfile} | sed s/ä/ae/`
    zipfile=`echo ${zipfile} | sed s/ö/oe/`
    zipfile=`echo ${zipfile} | sed s/ü/ue/`
    zipfile=`echo ${zipfile} | sed s/ß/ss/`
    # make zip
    zip -ruy0 "${zipfile}" "${appfile}"
done

EDIT

Got it!! The trick is to run the filename through iconv to convert the encoding. I did this before doing the äöü replacements and it worked:

zipfile=`echo ${zipfile} | iconv -f utf-8-mac -t utf-8`
+1  A: 

You probably need to use sed /s/ü/ue/g (note the g, global replace, so it replaces all occurrences, not just the first occurrence).

Are you trying to rename files? Or just change what the filename looks like for output?

EDIT

Try this script:

#/bin/bash

for appfile in *.app
do
    zipfile=`echo ${appfile} | sed s/app$/zip/`
    zipfile=`echo ${zipfile} | sed s/\ /_/g`
    # these don't work...
    zipfile=`echo ${zipfile} | sed s/ä/ae/g`
    zipfile=`echo ${zipfile} | sed s/ö/oe/g`
    zipfile=`echo ${zipfile} | sed s/ü/ue/g`
    zipfile=`echo ${zipfile} | sed s/ß/ss/g`
    # make zip
    echo zip -ruy0 "${zipfile}" "${appfile}"
done

When I run it with two simple filenames, it gives me output like this:

$ ./foo.sh
zip -ruy0 ae_oeoe_ue_ss.zip ä  öö ü ß.app
zip -ruy0 ueueueueue.zip üüüüü.app

The files are named ä öö ü ß.app and üüüüü.app.

I've just placed a g at the end of all the sed commands, and anchored the app. (Which will be nice when you've got one named appliance.app or deliciousapple.app.) The echo at the end is handy for testing. :)

There is something I don't understand: if your filenames have special characters in an iso-8859-1 locale (byte 0xDF for ß) and your command lines are in UTF-8 (bytes 0xC3 0x9F), what happens? How about the other way around?

sarnold
Basically I'm zipping up various files (individually) and I want to replace the special characters in the name of the zip (not the original files).
irlennard
Apparently, under Mac OS X special characters in filenames are stored in "decomposed form", meaning ä is stored as "a" + "COMBINING DIAERESIS" (U+0308). Any idea how I could match for that...?
irlennard
Woah, I hadn't known OS X stores (or generates?) filenames so differently than I expected. Could you use `sed s/\x61\x03\x08/ae/g` and so on?
sarnold
Found the answer. See my edited question above. Thanks for the app$ and global replace tips though!
irlennard