Avoiding slash in sed command

I am writing a shell script with this command:

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

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

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

How to ignore a slash so that the entire line FOLDER / OLD_ITEM is read correctly?

+4
source share
3 answers

You need to avoid /how \/.

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

Therefore use FOLDER\/OLD_ITEM

+5
source

You do not need to use /as a delimiter in sedregexps. You can use any character you like if it does not appear in the regular expression itself:

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

or

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

!

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

Source: https://habr.com/ru/post/1769191/


All Articles