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

55,519

Solution 1

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 >.

Solution 2

Note that "Windows ANSI" may not be CP1252 - that is configured by users.

Share:
55,519
user531912
Author by

user531912

Updated on February 10, 2020

Comments

  • user531912
    user531912 about 4 years

    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?

  • nwellnhof
    nwellnhof about 9 years
    The question asked specifically about the BOM, so I don't think your answer is helpful.
  • Fuhrmanator
    Fuhrmanator over 6 years
    Any idea why iconv from GnuWin is creating a UCS2 LE BOM encoded file (according to Notepad++ anyway) when I use -t UTF-8 ?
  • Fuhrmanator
    Fuhrmanator over 6 years