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
相关推荐
打不了嗝 ᥬ᭄6 分钟前
Linux的权限
linux
落幕11 分钟前
C语言-进程
linux·运维·服务器
深度Linux20 分钟前
C++程序员内功修炼——Linux C/C++编程技术汇总
linux·项目实战·c/c++
电棍2331 小时前
verilog笔记
笔记·fpga开发
让我安静会2 小时前
Obsidian·Copilot 插件配置(让AI根据Obsidian笔记内容进行对话)
人工智能·笔记·copilot
风静如云2 小时前
OpenBMC:BmcWeb定义service
linux
leoufung2 小时前
VIM FZF 安裝和使用
linux·编辑器·vim
世事如云有卷舒2 小时前
FreeRTOS学习笔记
笔记·学习
bugtraq20213 小时前
XiaoMi Mi5(gemini) 刷入Ubuntu Touch 16.04——安卓手机刷入Linux
linux·运维·ubuntu
CodeWithMe4 小时前
[ Vim ] 常用命令 and 配置
linux·编辑器·vim