问题描述
这是我的输入文本文件
< > & * ^ % $ # @ ! ) ( ) < > < > > > <
这是我正在使用的 sed shell 脚本。
sed 's/&/&/g ; s/</</g ; s/>/>/g' html_file.txt > new_file.txt
<lt; >gt; & * ^ % $ # @ ! ) ( ) <lt; >gt; <lt; >gt; >gt; >gt; <lt;
我不明白为什么还有 <
和 >
符号而不是 &
?
解决方法
来自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.
使用 &
将 \
转义到 \&
。