How to replace a path with another path in sed?

前端 未结 7 1593
广开言路
广开言路 2020-12-01 07:30

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

相关标签:
7条回答
  • 2020-12-01 08:03

    Using csh for serious scripting is usually not recommended. However, that is tangential to the issue at hand.

    You're probably after something like:

    sed -e "s=$oldpath=$newpath="
    

    where the shell variable $oldpath contains the value to be replaced and $newpath contains the replacement, and it is assumed that neither variable contains an equals sign. That is, you're allowed to choose the delimiter on pattern, and avoiding the usual / delimiter avoids problems with slashes in pathnames. If you think = might appear in your file names, choose something less likely to appear, such as control-A or control-G.

    0 讨论(0)
提交回复
热议问题