首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用sed从多个文件替换文件路径名

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

Stack Overflow用户
提问于 2018-07-24 05:25:37
回答 3查看 358关注 0票数 0

我想在多个文件中用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"/>

代码如下所示。

代码语言:javascript
复制
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 *

这里我得到了一个错误:

代码语言:javascript
复制
< was unexpected at this time.

请为我澄清什么在这里不起作用。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-24 05:32:49

请你试着跟我说一下,如果这对你有帮助的话,请告诉我。通过使用#作为sed的分隔符,您不需要在其中转义/,它只需要转义.?就不具有特殊意义。

代码语言:javascript
复制
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

对其进行了以下测试:

代码语言:javascript
复制
sed --version
GNU sed version 4.2.1
票数 3
EN

Stack Overflow用户

发布于 2018-07-24 14:50:48

#一起工作

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

票数 3
EN

Stack Overflow用户

发布于 2018-07-24 15:43:24

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

代码语言:javascript
复制
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标志,只有当您期望在一行中有多个匹配时,这才有意义。(也许你还是这么做的,在这种情况下,很明显是把它放回去了。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51491024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档