Batch convert files to UTF-8
In my daily job as DBA I need to convert a lot of files from ISO-8859-1 to UTF-8. This files are commonly SQL scripts generated on Windows environments and I have to execute them in linux servers (Red Hat or Ubuntu) against UTF-8 databases.These scripts usually have characters in Spanish and Catalan (tildes, ñ, ç, etc.) and I need to convert to UTF-8 to avoid getting errors due to invalid characters.
I’ve tried with iconv, specifying as output file the same name as the input file:
$ iconv -f ISO-8859-1 -t utf-8 file.sql -o file.sql
but if the file is greater than 32KB I get this:
Bus error
To avoid this I’ve created a script (2utf8.sh):
#!/bin/bash if [ $# -lt 1 ] then echo "Use: "$0" <file_name>" echo "Convert files from ISO-8859-1 to UTF8" exit fi for i in $* do if [ ! -f $i ]; then # Only convert text files continue fi # Generate temp file to avoid Bus error iconv -f ISO-8859-1 -t utf-8 $i -o $i.tmp mv $i.tmp $i done
You just have to run it with the name of the file to convert, or using wildcards:
$ 2utf8.sh *.sql
Hope this help you!
Posted 10 years ago and still useful thank you.
David
LikeLike
Wow, 10 years. I’m getting old 😉
LikeLike
Thanks, thanks a lot. I was getting in some files “Bus error (core dumped)” and I didn’t understand why… Then, I saw those files were being cut and some of the information was lost, but with your small script everything is correct.
Thank you very much, I really appreciate this.
Elena~ (⌒ω⌒)
LikeLike
Thanks for your comments, Elena 😉
LikeLike
Thank YOU! Echoing everything the two others who posted. You are a savior!
LikeLike
You’re wellcome Arya!
LikeLike
This helped so much. I was banging my head against the wall trying to figure out why I kept getting the “Bus Error” error.
LikeLike
You. are. AMAZING!
Thanks for this. I know not many people are going to run into these issues but you’ve honestly saved my butt. I would never have thought this was the reason why my file was limited to 32768 bytes.
If I ever run into you, I owe you a beer :)!
LikeLike
You’re welcome!
Hope to see you soon 😉
LikeLike