Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何直接替换ISO-8889字符和十六进制表示?_Bash_Awk_Sed_Utf 8_Iso 8859 1 - Fatal编程技术网

Bash 如何直接替换ISO-8889字符和十六进制表示?

Bash 如何直接替换ISO-8889字符和十六进制表示?,bash,awk,sed,utf-8,iso-8859-1,Bash,Awk,Sed,Utf 8,Iso 8859 1,我有一个HTML文件,我想将其ISO-8859-1代码转换为UTF-8 有时,在文件中,特殊字符以这种格式出现 &#x200 &#x203 È Ë 有时,特殊字符以这种格式出现 &#x200 &#x203 È Ë 在这两种情况下,我都希望用HTML标识替换它们,如下所示: È Ë 我试过使用awk这样做: awk '{gsub(/\200/, "\\È" , $0); print}

我有一个HTML文件,我想将其ISO-8859-1代码转换为UTF-8

有时,在文件中,特殊字符以这种格式出现

&#x200
&#x203
 È
 Ë
有时,特殊字符以这种格式出现

&#x200
&#x203
 È
 Ë
在这两种情况下,我都希望用HTML标识替换它们,如下所示:

 È
 Ë
我试过使用
awk
这样做:

awk '{gsub(/\200/, "\\È" , $0); print}' file
但在本例中,仅替换为
È
,而不是其等价物
Ȁ

是否有一种方法可以在单个/直接命令中替换这些字符,或者需要考虑这两种方式?我的意思是,对每个角色都做如下操作

awk '{ gsub(/\&#x200/, "\\È" , $0)
       gsub(/\200/,    "\\È" , $0); print}' file
如果有更有效的方法或使用其他工具,我愿意接受建议。提前感谢。

一种方法是使用perl模块(如果尚未安装,可通过OS package manager或CPAN获得)转换所有实体和非ASCII字符:

$ cat example.html
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Testing &#200; and &#203;
    <p>Testing È and Ë
  </body>
</html>
$ file example.html
example.html: HTML document, ISO-8859 text
$ perl -Mopen=IN,":encoding(iso-8859-1)" -MHTML::Entities -ne \
    'print encode_entities(decode_entities($_), "^\n\x20-\x25\x27-\x7e")' example.html
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Testing &Egrave; and &Euml;
    <p>Testing &Egrave; and &Euml;
  </body>
</html>
$cat example.html
测试È;和Ë;
测试È和Ë
$file example.html
example.html:html文档,ISO-8859文本
$perl-Mopen=IN,“:编码(iso-8859-1)”-MHTML::Entities-ne\
'打印编码实体(解码实体($),“^\n\x20-\x25\x27-\x7e”)'example.html
测试与电子政务;及Ë;
测试与电子政务;及Ë;
一种方法,使用perl模块(如果尚未安装,可通过OS package manager或CPAN获得)转换所有实体和非ASCII字符:

$ cat example.html
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Testing &#200; and &#203;
    <p>Testing È and Ë
  </body>
</html>
$ file example.html
example.html: HTML document, ISO-8859 text
$ perl -Mopen=IN,":encoding(iso-8859-1)" -MHTML::Entities -ne \
    'print encode_entities(decode_entities($_), "^\n\x20-\x25\x27-\x7e")' example.html
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <p>Testing &Egrave; and &Euml;
    <p>Testing &Egrave; and &Euml;
  </body>
</html>
$cat example.html
测试È;和Ë;
测试È和Ë
$file example.html
example.html:html文档,ISO-8859文本
$perl-Mopen=IN,“:编码(iso-8859-1)”-MHTML::Entities-ne\
'打印编码实体(解码实体($),“^\n\x20-\x25\x27-\x7e”)'example.html
测试与电子政务;及Ë;
测试与电子政务;及Ë;

你的意思是
和#200
Ë,顺便说一句
ȃ
是非常不同的字符。实际上在HTML文件中出现的
x
我不知道为什么。你的意思是
È
Ë,顺便说一句
ȃ
是非常不同的字符。实际上,HTML文件中出现了
x
我不知道为什么。非常好。非常感谢您的帮助。我必须安装HTML:Parser,我发现它包含HMTL:Entities,并且工作得非常好!。有一个问题,我对perl几乎一无所知,这部分内容是什么意思?
“^\n\x20-\x25\x27-\x7e”
?非常好。非常感谢您的帮助。我必须安装HTML:Parser,我发现它包含HMTL:Entities,并且工作得非常好!。有一个问题,我对perl几乎一无所知,这部分内容是什么意思?