iconv: Converting from Windows ANSI to UTF-8 with BOM

user531912 picture user531912 · Dec 6, 2010 · Viewed 53.3k times · Source

I want to use iconv to convert files on my Mac. The goal is to go from "Windows ANSI" to "whatever Windows Notepad saves, if you tell it to use UFT8".

This is what I want:

$ file names.csv 
names.csv: UTF-8 Unicode (with BOM) text, with CRLF line terminators

This is what I use:

$ iconv -f CP1252 -t UTF-8  names.csv > names.utf8.csv 

This is what I get (not what I want):

$ file names.utf8.csv 
names.utf8.csv: UTF-8 Unicode text, with CRLF line terminators

How do I get the BOM?

Answer

borrible picture borrible · Dec 6, 2010

You can add it manually by first echoing the bytes into the file:

echo -ne '\xEF\xBB\xBF' > names.utf8.csv

and then concatenating your required information at the end:

iconv -f CP1252 -t UTF-8  names.csv >> names.utf8.csv

Note the >> rather than >.