Linux 使用 iconv 将 UTF-16LE 转换为 UTF-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17287713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:18:00  来源:igfitidea点击:

Using iconv to convert from UTF-16LE to UTF-8

linuxsedgreputf-16le

提问by laitha0

Hi I am trying to convert some log files from a Microsoft SQL server, but the files are encoded using UTf-16LE and iconv does not seem to be able to convert them.

嗨,我正在尝试从 Microsoft SQL 服务器转换一些日志文件,但这些文件是使用 UTf-16LE 编码的,而 iconv 似乎无法转换它们。

I am doing:

我在做:

iconv -f UTF-16LE -t UTF-8 <filename>

I also tried to delete any carriage returns from the end of the line if there are any, but that did not fix it either. If I save it using gedit that works, but this is not a viable solution since I have hundreds of those files.

如果有的话,我还尝试从行尾删除任何回车,但这也没有解决。如果我使用有效的 gedit 保存它,但这不是一个可行的解决方案,因为我有数百个这样的文件。

EDIT: Please see the new answer for the missing option

编辑:请参阅缺少选项的新答案

回答by laitha0

I forgot the -oswitch!

我忘记-o开关了!

The final command is :

最后的命令是:

iconv -f UTF-16LE -t UTF-8 <filename> -o <new-filename>

回答by A. Blesius

The command you specified will output to stdout. You can either use the -oparameter, or redirect your output:

您指定的命令将输出到标准输出。您可以使用-o参数,也可以重定向输出:

with -o:

-o

iconv -f UTF-16LE -t UTF-8 infile -o outfile

with piping:

带管道:

iconv -f UTF-16LE -t UTF-8 infile > outfile

Both will yield the desired result.

两者都会产生预期的结果。

However some versions of iconv (v1 on macOS for example) do not support the -oparameter and you will see that the converted text is echoed to stdout. In that case, use the piping option.

但是,某些版本的 iconv(例如 macOS 上的 v1)不支持该-o参数,您将看到转换后的文本会回显到 stdout。在这种情况下,请使用管道选项。