开发者

How to escape the forward slash in a sed command [duplicate]

Thi开发者_开发百科s question already has answers here: Using different delimiters in sed commands and range addresses (3 answers) Closed 1 year ago.

I'm writing a shell script with this command:

sed -e 's/OLD_ITEM/NEW_ITEM/g' 

But I actually want to do something that includes a directory:

sed -e 's/FOLDER/OLD_ITEM/NEW_ITEM/g'

How do ignore the forward slash so that the entire line FOLDER/OLD_ITEM is read properly?


You don't have to use / as delimiter in sed regexps. You can use whatever character you like, as long as it doesn't appear in the regexp itself:

sed -e 's@FOLDER/OLD_ITEM@NEW_ITEM@g'

or

sed -e 's|FOLDER/OLD_ITEM|NEW_ITEM|g'


You need to escape the / as \/.

The escape (\) preceding a character tells the shell to interpret that character literally.

So use FOLDER\/OLD_ITEM


Escape it !

sed -e 's/FOLDER\/OLD_ITEM/NEW_ITEM/g'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜