Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

How to replace a path with another path in sed?

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

sed s/AAA/BBB/ file

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

s+AAA+BBB+

and so on.

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')

and then do what you need with $pwdesc.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...