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

实测:

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

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 18:02:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 18:02:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 18:02:03       57 阅读
  4. Python语言-面向对象

    2024-07-15 18:02:03       68 阅读

热门阅读

  1. 网络协同新纪元:Eureka引领分布式网络管理革命

    2024-07-15 18:02:03       19 阅读
  2. deepstream tracker NvDCF未实现跟踪

    2024-07-15 18:02:03       19 阅读
  3. Mybatis

    Mybatis

    2024-07-15 18:02:03      13 阅读
  4. Kafka 入门指南

    2024-07-15 18:02:03       14 阅读
  5. 【redis】redis发布/订阅模型

    2024-07-15 18:02:03       22 阅读
  6. 理解前端内存泄露

    2024-07-15 18:02:03       25 阅读
  7. Spring Boot和Spring有什么区别

    2024-07-15 18:02:03       15 阅读
  8. python2与python3中的subprocess.Popen差异

    2024-07-15 18:02:03       19 阅读
  9. 面向开发者的提示词工程第一章-简介

    2024-07-15 18:02:03       22 阅读
  10. Python网页开发的常用框架

    2024-07-15 18:02:03       20 阅读