如何以直接方式替换 ISO-8889 字符和十六进制表示?
How to replace ISO-8889 characters and hex representation in direct way?
我有一个 HTML 文件,我想将其 ISO-8859-1 代码转换为 UTF-8。
有时,在文件中,特殊字符以这种格式出现
Ȁ
ȃ
有时,特殊字符会以这种格式出现
È
Ë
在这两种情况下,我都想用 HTML 标识替换它们,如下所示:
È
Ë
我试过 awk
这样做:
awk '{gsub(/0/, "\È" , [=13=]); print}' file
但在这种情况下,only 被替换 È
而不是它的等价物 Ȁ
.
有没有办法替换 single/direct 命令中的那些字符,或者需要同时考虑这两种方式?我的意思是,对每个字符都执行以下操作?
awk '{ gsub(/\Ȁ/, "\È" , [=14=])
gsub(/0/, "\È" , [=14=]); print}' file
如果有更有效的方法或使用其他工具,我愿意接受建议。提前致谢。
一种方法,使用 perl HTML::Entities 模块(可通过 OS 包管理器或 CPAN,如果尚未安装)转换 all 实体和非 ASCII 字符:
$ cat example.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Testing È and Ë
<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 È and Ë
<p>Testing È and Ë
</body>
</html>
我有一个 HTML 文件,我想将其 ISO-8859-1 代码转换为 UTF-8。
有时,在文件中,特殊字符以这种格式出现
Ȁ
ȃ
有时,特殊字符会以这种格式出现
È
Ë
在这两种情况下,我都想用 HTML 标识替换它们,如下所示:
È
Ë
我试过 awk
这样做:
awk '{gsub(/0/, "\È" , [=13=]); print}' file
但在这种情况下,only 被替换 È
而不是它的等价物 Ȁ
.
有没有办法替换 single/direct 命令中的那些字符,或者需要同时考虑这两种方式?我的意思是,对每个字符都执行以下操作?
awk '{ gsub(/\Ȁ/, "\È" , [=14=])
gsub(/0/, "\È" , [=14=]); print}' file
如果有更有效的方法或使用其他工具,我愿意接受建议。提前致谢。
一种方法,使用 perl HTML::Entities 模块(可通过 OS 包管理器或 CPAN,如果尚未安装)转换 all 实体和非 ASCII 字符:
$ cat example.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Testing È and Ë
<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 È and Ë
<p>Testing È and Ë
</body>
</html>