sed的替换可用:斜杠/,竖或|,井号#,@,感叹号!, 等符号, 但是查找只能用斜杠/ , sed的查找和替换可以一起用 笔记240718

122 阅读2分钟

sed的替换可用:斜杠/,竖或|,井号#,@,感叹号!, 等符号

WSL Ubuntu22.04 环境下测试

z@4800H:~$ echo "hello" | sed 's/l/L/g'
echo "hello" | sed 's`l`L`g'
echo "hello" | sed 's~l~L~g'
echo "hello" | sed 's|l|L|g'
echo "hello" | sed 's!l!L!g'
echo "hello" | sed 's@l@L@g'
echo "hello" | sed 's#l#L#g'
echo "hello" | sed 's$l$L$g'
echo "hello" | sed 's%l%L%g'
echo "hello" | sed 's^l^L^g'
echo "hello" | sed 's&l&L&g'
echo "hello" | sed 's*l*L*g'
echo "hello" | sed 's(l(L(g'
echo "hello" | sed 's)l)L)g'
echo "hello" | sed 's_l_L_g'
echo "hello" | sed 's-l-L-g'
echo "hello" | sed 's+l+L+g'
echo "hello" | sed 's=l=L=g'
echo "hello" | sed 's1l1L1g'
echo "hello" | sed 's2l2L2g'
echo "hello" | sed 's3l3L3g'
echo "hello" | sed 'salaLag'
echo "hello" | sed 'sblbLbg'
echo "hello" | sed 'sclcLcg'
echo "hello" | sed 'sDlDLDg'
echo "hello" | sed 'sslsLsg'
echo hello | sed 's"l"L"g'
echo hello | sed "s'l'L'g"
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo
heLLo

centOS6测试

[root@c60min ~]# echo "hello" | sed 's/l/L/g'
heLLo
[root@c60min ~]# echo "hello" | sed 's`l`L`g'
heLLo
[root@c60min ~]# echo "hello" | sed 's~l~L~g'
heLLo
[root@c60min ~]# echo "hello" | sed 's|l|L|g'
heLLo
[root@c60min ~]# echo "hello" | sed 's!l!L!g'
heLLo
[root@c60min ~]# echo "hello" | sed 's@l@L@g'
heLLo
[root@c60min ~]# echo "hello" | sed 's#l#L#g'
heLLo
[root@c60min ~]# echo "hello" | sed 's$l$L$g'
heLLo
[root@c60min ~]# echo "hello" | sed 's%l%L%g'
heLLo
[root@c60min ~]# echo "hello" | sed 's^l^L^g'
heLLo
[root@c60min ~]# echo "hello" | sed 's&l&L&g'
heLLo
[root@c60min ~]# echo "hello" | sed 's*l*L*g'
heLLo
[root@c60min ~]# echo "hello" | sed 's(l(L(g'
heLLo
[root@c60min ~]# echo "hello" | sed 's)l)L)g'
heLLo
[root@c60min ~]# echo "hello" | sed 's_l_L_g'
heLLo
[root@c60min ~]# echo "hello" | sed 's-l-L-g'
heLLo
[root@c60min ~]# echo "hello" | sed 's+l+L+g'
heLLo
[root@c60min ~]# echo "hello" | sed 's=l=L=g'
heLLo
[root@c60min ~]# echo "hello" | sed 's1l1L1g'
heLLo
[root@c60min ~]# echo "hello" | sed 's2l2L2g'
heLLo
[root@c60min ~]# echo "hello" | sed 's3l3L3g'
heLLo
[root@c60min ~]# echo "hello" | sed 'salaLag'
heLLo
[root@c60min ~]# echo "hello" | sed 'sblbLbg'
heLLo
[root@c60min ~]# echo "hello" | sed 'sclcLcg'
heLLo
[root@c60min ~]# echo "hello" | sed 'sDlDLDg'
heLLo
[root@c60min ~]# echo "hello" | sed 'sslsLsg'
heLLo
[root@c60min ~]# echo hello | sed 's"l"L"g'
heLLo
[root@c60min ~]# echo hello | sed "s'l'L'g"
heLLo

sed的替换可用:斜杠/,竖或|,井号#,@,感叹号!, 等符号, 但是... ... 查找只能用斜杠/

替换必须用s开头, 如:s/ , s| , s#

例如:

  • s/正则/替换内容/
  • s/正则/替换内容/g
  • s|正则|替换内容|
  • s|正则|替换内容|g
  • s#正则#替换内容#
  • s#正则#替换内容#g

当内容包含斜杠/时, (例如路径) , 使用 竖或|,井号# 比较方便, 可以不用转义路径分隔符斜杠/


与替换相比, 查找只能用斜杠 / ,    sed '/hello/ 不能写成 sed '|hello|' 或 sed '#hello#'

ip addr|sed '/inet /' 效果类似 ip addr|grep 'inet '



sed的查找和替换可以一起用

sed的查找和替换可以一起用, 先用查找过滤一部分内容, 再在剩余的内容中执行替换.

查找只能用/ , 例如:
将所有包含"hello"的行中的"world"替换成"世界" , 可写成:

  • /hello/s/world/世界/
  • /hello/s/world/世界/g
  • /hello/s|world|世界|
  • /hello/s|world|世界|g
  • /hello/s#world#世界#
  • /hello/s#world#世界#g

实测:

tempStringVar="$(echo -e "
hello world world world 
world world world world 
hello world world world 
world world world world 
hello world world world 
world world world world 
")"
echo "${tempStringVar}" | sed   '/hello/s/world/世界/'
echo "${tempStringVar}" | sed   '/hello/s/world/世界/g'
echo "${tempStringVar}" | sed   '/hello/s|world|世界|'
echo "${tempStringVar}" | sed   '/hello/s|world|世界|g'
echo "${tempStringVar}" | sed   '/hello/s#world#世界#'
echo "${tempStringVar}" | sed   '/hello/s#world#世界#g'

结果:

[root@1235vm-c69w yum.repos.d]# tempStringVar="$(echo -e "
> hello world world world 
> world world world world 
> hello world world world 
> world world world world 
> hello world world world 
> world world world world 
> ")"
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s/world/世界/'

hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s/world/世界/g'

hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s|world|世界|'

hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s|world|世界|g'

hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s#world#世界#'

hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s#world#世界#g'

hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 
hello 世界 世界 世界 
world world world world 

实测2

tempStringVar="
hello world world world 
world world world world "
echo "${tempStringVar}" | sed   '/hello/s/world/世界/'
echo "${tempStringVar}" | sed   '/hello/s/world/世界/g'
echo "${tempStringVar}" | sed   '/hello/s|world|世界|'
echo "${tempStringVar}" | sed   '/hello/s|world|世界|g'
echo "${tempStringVar}" | sed   '/hello/s#world#世界#'
echo "${tempStringVar}" | sed   '/hello/s#world#世界#g'

结果

[root@1235vm-c69w yum.repos.d]# tempStringVar="
> hello world world world 
> world world world world "
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s/world/世界/'

hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s/world/世界/g'

hello 世界 世界 世界 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s|world|世界|'

hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s|world|世界|g'

hello 世界 世界 世界 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s#world#世界#'

hello 世界 world world 
world world world world 
[root@1235vm-c69w yum.repos.d]# echo "${tempStringVar}" | sed   '/hello/s#world#世界#g'

hello 世界 世界 世界 
world world world world