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

查看:37
本文介绍了如何用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天全站免登陆