更改txt文件的编码

Pie*_*rre 16 encoding bash echo

当我写:

file file1.txt 
Run Code Online (Sandbox Code Playgroud)

我有这个输出:Little-endian UTF-16 Unicode text, with CR line terminators

然后如果我写:

file file2.txt 
Run Code Online (Sandbox Code Playgroud)

我有: ASCII 文本

file2.txt 是通过以下方式创建的:

echo $var > "file2.txt"
Run Code Online (Sandbox Code Playgroud)

我希望 file2.txt 与 file1.txt 具有相同的编码。我怎样才能做到这一点 ?

Der*_*ler 28

您可以使用iconv来转换文件的编码:

iconv -f ascii -t utf16 file2.txt > another.txt
Run Code Online (Sandbox Code Playgroud)

another.txt 然后应该有所需的编码。

你也可以试试这个:

echo $var | iconv -f ascii -t utf16 > "file2.txt"
Run Code Online (Sandbox Code Playgroud)


Alu*_* G. 8

使用 iconv:

echo "$var" | iconv --from-code=utf-8 --to-code=utf-16le --output=file2.txt
Run Code Online (Sandbox Code Playgroud)