bash 在 shell 中将 ANSI 转换为 UTF-8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20263909/
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-09-10 00:28:25  来源:igfitidea点击:

Converting ANSI to UTF-8 in shell

bashshellcsvutf-8iconv

提问by Neringan

I'm making a parser (1 csv to 3 csv) script and I have a problem. I am French so in my language I have letters like: é è à ....

我正在制作一个解析器(1 个 csv 到 3 个 csv)脚本,但遇到了问题。我是法国人,所以在我的语言中,我有这样的字母:é è à ....

A customer sent me a csv file that Linux recognizes as "unknown-8bit" (ansi I guess).

一位客户向我发送了一个 csv 文件,Linux 将其识别为“未知 8 位”(我猜是 ansi)。

In my script, I'm writing 3 new csv files. But ViM creates them as ISO latin1 because it's close to what it got in the entry, but my é,è,à...are broken. I need UTF-8.

在我的脚本中,我正在编写 3 个新的 csv 文件。但是 ViM 将它们创建为 ISO latin1,因为它接近条目中的内容,但我é,è,à...的坏了。我需要UTF-8。

So I tried to convert the first ANSI csv to UTF-8 :

所以我尝试将第一个 ANSI csv 转换为 UTF-8 :

iconv -f "windows-1252" -t "UTF-8" import.csv -o import.csv

The problem is that it breaks my CSV. It's now on only one row. But my special chars are ok. Is there a way to convert ANSI to UTF-8 and keeping my rows?

问题是它破坏了我的 CSV。现在只有一排。但是我的特殊字符还可以。有没有办法将 ANSI 转换为 UTF-8 并保留我的行?

回答by Grzegorz ?ur

Put the output into another file. Don't overwrite the old one.

将输出放入另一个文件。不要覆盖旧的。

iconv -f "windows-1252" -t "UTF-8" import.csv -o new_import.csv

iconvfails when reading and writing to the same file.

iconv读取和写入同一文件时失败。