iconv:使用BOM从Windows ANSI转换为UTF-8

use*_*912 29 unicode iconv

我想用iconv来转换Mac上的文件.目标是从"Windows ANSI"转到"Windows Notepad保存的任何内容,如果你告诉它使用UFT8".

这就是我要的:

anders-johansen-privats-macbook-pro:test andersprivat$ file names.csv 
names.csv: UTF-8 Unicode (with BOM) text, with CRLF line terminators
Run Code Online (Sandbox Code Playgroud)

这是我使用的:

iconv -f CP1252 -t UTF-8  names.csv > names.utf8.csv 
Run Code Online (Sandbox Code Playgroud)

这就是我得到的(不是我想要的):

file names.utf8.csv 
names.utf8.csv: UTF-8 Unicode text, with CRLF line terminators
Run Code Online (Sandbox Code Playgroud)

我如何获得BOM?

bor*_*ble 43

您可以echo通过将字节首先放入文件来手动添加它:

echo -ne '\xEF\xBB\xBF' > names.utf8.csv
Run Code Online (Sandbox Code Playgroud)

然后在最后连接您所需的信息:

iconv -f CP1252 -t UTF-8  names.csv >> names.utf8.csv
Run Code Online (Sandbox Code Playgroud)

注意>>而不是>.