我有一个文件1.htm.我想替换一个字母ṣ(带有以下点的s).我尝试使用sed和perl,它不会替换.
sed -i 's/?/s/g' "1.htm"
perl -i -pe 's/?/s/g' "1.htm"
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议做什么
1.html(不替换ṣ)
我也发现了另一件奇怪的事情.Sed(与上面相同的命令)替换在一个文件中但不替换另一个我放置链接
unreplacable.html与1.html相同
为什么会这样.sed能够在一个文件中替换ṣ而不能在另一个文件中替换.
您在html文件中组合了字符.也就是说,"?"实际上是"s"一个" ?"(一个COMBINING DOT BELOW).修复oneliner的一种可能性是:
perl -C -i -pe 's/s\x{0323}/s/g' "1.htm"
Run Code Online (Sandbox Code Playgroud)
也就是说,将stdout/stdin的utf8模式转换为(-C)并明确地写入左侧的两个字符s///.
另一种可能性是使用Unicode::Normalize例如:标准化所有组合字符:
perl -C -MUnicode::Normalize -Mutf8 -i -pe '$_=NFKC($_); s/?/s/g' "1.htm"
Run Code Online (Sandbox Code Playgroud)
但这也会规范化输入文件中的所有其他字符,这对您来说可能适用也可能不适用.