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

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

实测:

bash 复制代码
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'

结果:

bash 复制代码
[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

bash 复制代码
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'

结果

bash 复制代码
[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
相关推荐
小脑斧爱吃鱼鱼9 分钟前
鸿蒙项目笔记(2)
笔记·学习·华为·harmonyos
林政硕(Cohen0415)26 分钟前
RK3568 基于Gstreamer的多媒体调试记录
linux·rk3568·多媒体·gstreamer
陈奕迅本讯1 小时前
操作系统 4.4-从生磁盘到文件
linux·运维·服务器
0基础学习者2 小时前
按键消抖(用状态机实现)
前端·笔记·fpga开发·verilog·fpga
湫qiu2 小时前
为什么我建议你主动释放直接内存 - Java
java·linux·后端
GLAB-Mary3 小时前
你的 Linux 服务器连不上网?10 分钟入门网络故障排查
linux·服务器·php
开发小能手-roy3 小时前
如何设置Ubuntu服务器版防火墙
linux·运维·ubuntu
天天年年天天。3 小时前
在 Linux 或 Unix 系统中使用 pthread_create 创建新线程的步骤
linux·数据结构
东方芷兰4 小时前
JavaWeb 课堂笔记 —— 08 请求响应
xml·java·笔记·spring·tomcat·html·idea