简体   繁体   English

如何使用 SED 命令查找和替换文件路径(以及特殊字符)

[英]How to find and replace a file path (along with special characters) using SED command

How to find and replace a file path like a/b/c/d into x/y/z using sed command?如何使用sed命令查找a/b/c/d等文件路径并将其替换为x/y/z

I tried sed -i -e 's/a/\\b/\\c/\\d/x/\\y/\\z' file.txt but it did not work.我试过sed -i -e 's/a/\\b/\\c/\\d/x/\\y/\\z' file.txt但它没有用。

Just use another separator for s command.只需为s命令使用另一个分隔符。 You can use any character.您可以使用任何字符。 Just / is the most popular one.只是/是最受欢迎的。 I like @ or # .我喜欢@# Then you have to escape that character.然后你必须逃避那个角色。

sed 's@a/b/c/d@x/y/z@'

but it did not work但它没有用

To escape / use \\/ not /\\ .转义/使用\\/而不是/\\ The \\ needs to be in front of / . \\需要在/ You could:你可以:

sed 's/a\/b\/c\/d/x\/y\/z/'

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

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