如何用sed中的另一个路径替换一个路径? [英] How to replace a path with another path in sed?

查看:841
本文介绍了如何用sed中的另一个路径替换一个路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在以下位置使用csh脚本(尽管如果有相关性,我可以更改语言):

I have a csh script (although I can change languages if it has any relevance) where I have to:

sed s/AAA/BBB/ file

问题在于AAA和BBB是路径,因此包含'/'. AAA是固定的,所以我可以说:

The problem is that AAA and BBB are paths, and so contain '/'. AAA is fixed, so I can say:

sed s/\\\/A\\\/A\\\A/BBB/ file

但是,BBB基于变量,包括$ PWD.如何在$ PWD中转义'/'?

However, BBB is based on variables, including $PWD. How do I escape the '/' in $PWD?

或者还有其他方法我应该完全做到这一点吗?

OR is there some other way I should be doing this entirely?

推荐答案

sed可以在s命令中使用任何分隔符代替/.只需使用您的路径中未遇到的东西即可:

sed can use any separator instead of / in the s command. Just use something that is not encountered in your paths:

s+AAA+BBB+

以此类推.

或者(如果您不想猜测),您可以使用sed预处理路径以避开斜杠:

Alternatively (and if you don't want to guess), you can pre-process your path with sed to escape the slashes:

pwdesc=$(echo $PWD | sed 's_/_\\/_g')

,然后使用$pwdesc做您需要的事情.

and then do what you need with $pwdesc.

这篇关于如何用sed中的另一个路径替换一个路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆