简体   繁体   English

使用 sed 命令将输入文本文件中出现的所有字符“&”、“<”、“>”替换为其 HTML 实体

[英]Using sed command replace in the input text file all occurrences of characters '&', '<', '>' with their HTML entities

this is my input text file这是我的输入文本文件

< > & * ^ % $ # @ ! ) ( ) < > < > > > < 

This is the sed shell script that I am using.这是我正在使用的 sed shell 脚本。

sed 's/&/&amp;/g ; s/</&lt;/g ; s/>/&gt;/g' html_file.txt > new_file.txt

This is the output file:这是 output 文件:

<lt; >gt; &amp; * ^ % $ # @ ! ) ( ) <lt; >gt; <lt; >gt; >gt; >gt; <lt; 

I can't understand that why there is still < and > signs instead of & ?我不明白为什么仍然有<>符号而不是&

From info sed :来自info sed

3.3 The 's' Command
[...]
The 's' command (as in substitute) is probably the most important in
'sed' [...]. The syntax of the 's' command is 's/REGEXP/REPLACEMENT/FLAGS'.
[...]
The REPLACEMENT can contain [...] unescaped '&' characters which reference the
whole matched portion of the pattern space.

Escape & with \ to \& .\转义&\&

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM