【问题标题】:Formating file changes encoding on Redhat system格式化文件更改 Redhat 系统上的编码
【发布时间】:2021-05-26 06:59:59
【问题描述】:

我有一个从 oracle 数据库中提取数据的 bash 脚本。我使用假脱机来提取数据。提取后,我通过删除和替换一些字符来格式化文件。我的问题是格式化后的文件是 ANSI 编码而不是 ut8。

  1. 使用线轴提取。文件是utf8
  2. 用 cat 和 tr 命令格式化并重定向到另一个文件。这个文件是 ansi。

相同的过程在 Aix 系统上运行良好。我尝试了iconv,但它不起作用。您知道为什么编码从 utf8 更改为 ansi 吗?请问如何改正?

【问题讨论】:

  • 恐怕您(无意中)损坏了自己的文件。请提供更多详细信息。从echo $NLS_LANG开始
  • 感谢您的回复。 $NLS_LANG 是 FRENCH_FRANCE.UTF8
  • 到目前为止一切顺利。现在请编辑您的原始帖子并引用导致问题的“格式”。

标签: redhat ansi file-format


【解决方案1】:

因此,您应该使用 ISO-8859-1 或 UTF-8。在后一种情况下,不要使用tr,因为它(还没有?)支持多字节字符,请改用sed(例如sed 's/deletethis//g')。

ISO-8859-1:

export LC_CTYPE=fr_FR.ISO-8859-1
export NLS_LANG=French_France.WE8ISO8859P1

# fetch data from Oracle, emulated by the following line
echo 'âêîôû' >test.latin1 # 5 bytes (+lineend)

# perform formatting, eg:
sed 's/ê/[e-circumflex]/g' test.latin1

# or the same with hex-codes:
sed $'s/\xea/[e-circumflex]/g' test.latin1

UTF-8:

export LC_CTYPE=fr_FR.UTF-8
export NLS_LANG=French_France.AL32UTF8

# fetch data from Oracle, emulated by the following line
echo 'âêîôû' >test.utf8 # 10 bytes (+lineend)

# perform formatting, eg:
sed 's/ê/[e-circumflex]/g' test.utf8

# or the same with hex-codes:
sed $'s/\xc3\xaa/[e-circumflex]/g' test.utf8

注意:不需要转换(iconvrecode 等),只需确保NLS_LANGLC_CTYPE 兼容。 (此外,您的终端(模拟器)应进行相应设置;对于 PuTTY,它是 Configuration/Category/Window/Translation/Remote-character-set。)

原答案:

我无法判断您执行的格式有什么问题,但这里有一个损坏 utf8 编码文本的方法:

$ echo 'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP' | iconv -f iso-8859-2 -t utf-8 | xxd
00000000: c381 5256 c38d 5a54 c5b0 52c5 9020 54c3  ..RV..ZT..R.. T.
00000010: 9c4b c396 5246 c39a 52c3 9347 c389 500a  .K..RF..R..G..P.

$ echo 'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP' | iconv -f iso-8859-2 -t utf-8 | tr -d $'\200-\237' | xxd
00000000: c352 56c3 5a54 c5b0 52c5 2054 c34b c352  .RV.ZT..R. T.K.R
00000010: 46c3 52c3 47c3 500a                      F.R.G.P.

这里tr -d $'\200-\237' 部分删除了一半的 utf8 序列(c381 变为 c3,c590 变为 c5),导致文本无法使用。

【讨论】:

  • 感谢您的回复。这是我做的格式: cat File | tr -d '\n' |tr -d '\r' | tr '°' ' ' | tr -d''| tr -d 'Â' | grep -v '^$' | iconv -f ISO8859-1 -t UTF-8>File.out
猜你喜欢
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-08-29
  • 2012-01-03
  • 2017-06-24
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多