我们可以使用命令“sed”将一个字符串替换为另一个包含字符“/”的字符串吗?

1 command-line

我尝试将“_”替换为“/”

$ echo "hahahaa_the_cat_hahahaa" | sed "s/_///g"
Run Code Online (Sandbox Code Playgroud)

它不起作用。

ter*_*don 5

替换运算符 ( s/old/new/) 可以使用任何字符作为分隔符:

$ echo foo | sed 's|f|g|'
goo
$ echo foo | sed 'safaga'
goo
Run Code Online (Sandbox Code Playgroud)

因此,只需使用任何不是的东西,/您就可以做您想做的事:

$ echo "hahahaa_the_cat_hahahaa" | sed 's|_|/|g'
hahahaa/the/cat/hahahaa
Run Code Online (Sandbox Code Playgroud)

或者,您可以/使用\(write \/)转义:

$ echo "hahahaa_the_cat_hahahaa" | sed 's/_/\//g'
hahahaa/the/cat/hahahaa
Run Code Online (Sandbox Code Playgroud)