LF - > UTF-16文件的CR / LF转换

时间:2015-07-30 12:11:22

标签: linux encoding command-line utf-16 line-endings

我有一个UTF-16编码文件,我想用Windows行结尾替换UNIX行结尾。我不想碰任何其他事情。

是否有一个linux命令行工具可以搜索两个字节" 0A 00"并将其替换为四个字节" 0D 00 0A 00"?

4 个答案:

答案 0 :(得分:1)

Perl救援:

perl -we 'binmode STDIN,  ":encoding(UTF-16le)";
          binmode STDOUT, ":encoding(UTF-16le):crlf";
          print while <STDIN>;
        ' < input.txt > output.txt

答案 1 :(得分:0)

unix2dos是您正在寻找的。查看其不同的选项,找到适合您的UTF-16编码的选项。

答案 2 :(得分:0)

您可以使用unix2dos,但您必须先将文件转换为8位编码,然后再将其转换为UTF-16。明显的中间候选人是UTF-8:

$ cat in.txt | iconv -f UTF-16 -t UTF-8 | unix2dos | iconv -f UTF-8 -t UTF-16 > out.txt

如果愿意,您可以将这三个管道命令包装在一个方便的脚本中。

#/bin/sh
iconv -f UTF-16 -t UTF-8 | unix2dos | iconv -f UTF-8 -t UTF-16

答案 3 :(得分:0)

解决方案:

perl -pe "BEGIN { binmode $_, ':raw:encoding(UTF-16LE)' for *STDIN, *STDOUT }; s/\n\0/\r\0\n\0/g;" < input.file > output.file

感谢我的同事Manu和Stream-process UTF-16 file with BOM and Unix line endings in Windows perl

相关问题