我看到了以下片段,这些片段讲述了逃离斜杠的不同方法:
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's//usr/local/bin//common/bin/'
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's_/usr/local/bin_/common/bin_'
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's:/usr/local/bin:/common/bin:'
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's|/usr/local/bin|/common/bin|'
输出为:
/dir1/dir2/common/bin/dir3
/dir1/dir2/common/bin/dir3
/dir1/dir2/common/bin/dir3
/dir1/dir2/common/bin/dir3
我的疑问是,为什么在最后三种方法的末尾没有斜杠。我试着把第二个放在最后:
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's//usr/local/bin//common/bin/'
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's_/usr/local/bin_/common/bin_/'
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's:/usr/local/bin:/common/bin:'
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's|/usr/local/bin|/common/bin|'
但出现了一个错误:
/dir1/dir2/common/bin/dir3
sed: -e expression #1, char 30: unknown option to `s'
/dir1/dir2/common/bin/dir3
/dir1/dir2/common/bin/dir3
这些片段显示的是,基本上可以使用任何字符作为命令的分隔符。s
是命令,后面的字符是分隔符
用于将";选项";对于彼此不同的CCD_ 2。
s
有三个选项,一个是要替换的东西(regex(,另一个是用(字符串(替换的东西,还有像g
这样的标志
您在最后一个位置添加了一个/
,它被解释为一个标志,但/
不是一个有效的标志。这就是造成错误的原因。
echo '/dir1/dir2/usr/local/bin/dir3' | sed 's_/usr/local/bin_/common/bin_/'
^^^ ^^ ^^
123 24 25
1: the command
2: the delimieter
3: the first option
4: the second option
5: the flag sed does not understand
当您使用非斜线的东西作为分隔符时,斜线对sed
没有任何特殊意义。