使用sed从多个文件中替换文件路径名

replace file path name from multiple file using sed

我想在多个文件中用 null 替换 <lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>

代码如下。

sed -i s|<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>||g *

这里我收到这个错误:

< was unexpected at this time.

请为我说明此处不起作用的地方。

能否请您尝试关注,如果这对您有帮助,请告诉我。使用#作为sed的分隔符你不需要转义/里面只需要转义.,?不要取他们的特殊含义

sed -E 's#<lexicon uri="file://C:/image/png/grammars/custom/image-custom\.lex\?SWI\.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom\.lex\?SWI\.type=backup"/>##' Input_file

测试:

sed --version
GNU sed version 4.2.1

#
合作 sed -i -e 's#<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>##g' test.txt

模式包含shell个元字符,需要用引号或转义。通常,在 Bash 中,您应该在字符串周围使用单引号,除非您需要 shell 来插入变量和命令替换并解释反斜杠序列(在这种情况下使用双引号)或执行空格标记化和通配符扩展(在这种情况下不使用引号)。另见 When to wrap quotes around a shell variable?

sed -i 's|<lexicon uri="file://C:/image/png/grammars/custom/image-custom.lex?SWI.type=backup"/><lexicon uri="file://C:/image/jpg/grammars/custom/image-dot-custom.lex?SWI.type=backup"/>||' *

我还去掉了 g 标志,只有当你希望在一行中有多个匹配项时才有意义。 (也许你毕竟做了,在这种情况下显然把它放回去了。)