0.sed 常用内部命令
a 在匹配后面添加
i 在匹配前面添加
p 打印
d 删除
s 查找替换
c 更改
y 转换 N D P
下面用a来演示
1.sed 'a 追加内容' 文件
# sed 'a newaddcontent' t.txt
1 the quick brown fox jumps over the lazy dog.
newaddcontent
2 the quick brown fox jumps over the lazy dog.
newaddcontent
3 the quick brown fox jumps over the lazy dog.
newaddcontent
如果执行在第3行追加,则可
# sed '3a newaddcontent' t.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
newaddcontent
如果要在第1行到第3行 追加则
# sed '1,3a newaddcontent' t.txt
1 the quick brown fox jumps over the lazy dog.
newaddcontent
2 the quick brown fox jumps over the lazy dog.
newaddcontent
3 the quick brown fox jumps over the lazy dog.
newaddcontent
4 the quick brown fox jumps over the lazy dog.
但实际的使用中我们并不知道行号,此时可以使用匹配模式
sed '/搜索内容/ a 追加内容' t.txt
# sed '/2 the/ a newcontent' t.txt
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
newcontent
2.关于 d 删除命令
sed -r '/^#/d' ~/nginx.conf #删除配置文件中以#开头的行号 -r为使用正则,//为匹配模式
sed -r '/(^#|#|^$)/d' ~/nginx.conf #增强版 不仅仅是匹配 #开头 还匹配了 包含# 及 空格
3.替换
# sed 's dog cat ' t.txt
1 the quick brown fox jumps over the lazy cat.
# sed '/2 the/s/dog/cat/' t.txt #匹配模式 找到2 the 这一行 然后替换
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
4.整行替换
# sed 'c zhenghang' t.txt #全部替换
zhenghang
zhenghang
# sed '2c zhenghang' t.txt #仅替换第二行
1 the quick brown fox jumps over the lazy dog.
zhenghang
3 the quick brown fox jumps over the lazy dog.
# sed '2,3c zhenghang' t.txt #替换第二到第三行
1 the quick brown fox jumps over the lazy dog.
zhenghang
4 the quick brown fox jumps over the lazy dog.
sed '/3 the/c zhenghang' t.txt #匹配模式替换整行
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
zhenghang
4 the quick brown fox jumps over the lazy dog.
5.转换
# sed 'y abcdefg ABCDEFG ' t.txt #将文中的前面的内容替换为后面的内容
1 thE quiCk Brown Fox jumps ovEr thE lAzy DoG.
6.打印
# sed '2p' t.txt #将第二行多打印一次 ,其余用法和上面类似
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
7.标志位
数字
g
p
w filename
如果要替换的内容有多个,默认会替换第一个,如果要指定替换某一个,则可以在后面加上指定的数字
# sed 's/dog/cat/' t2.txt #默认替换第一个dog
1 the quick brown fox jumps over the lazy cat dog.
# sed 's/dog/cat/2' t2.txt #在后面加上2则替换第二个dog
1 the quick brown fox jumps over the lazy dog cat.
# sed 's/dog/cat/g' t2.txt #使用g参数则替换所有
1 the quick brown fox jumps over the lazy cat cat.
# sed '2s/dog/cat/w newfile' t.txt #仅修改第二行并仅将第二行保存到新文件 newfile中
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy dog.
# cat newfile #可见在newfile中只有第二行,原文件未受影响
2 the quick brown fox jumps over the lazy cat.
8.命令选项
-n 抑制输出
# sed '3s/dog/cat/' t.txt 默认会显示文本中的所有行,但是我们只想显示第3行
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy dog.
# sed -n '3s/dog/cat/p' t.txt #可配合p使用,达到仅显示要显示的内容
3 the quick brown fox jumps over the lazy cat.
-e 执行多条命令
# sed -e 's/dog/cat/;s/brown/green/' t.txt
1 the quick green fox jumps over the lazy cat.
-f 指定命令文件位置
可以向上面那样将书写一条命令完成任务,也可以将多条命令存放在一个文件中,再通过使用 -f 指定命令文件位置,来达到同样的效果
# cat mingling.txt
s/brown/green/
s/dog/cat/
# sed -f mingling.txt t.txt
1 the quick green fox jumps over the lazy cat.
-i 修改源文件
注意!!! 此操作不可逆,要慎重使用
如果不加-i 则仅仅是将文件读到内存中显示,并不会写回原文件中,所以需要通过使用-i参数来写回原文件
# sed -ie 's/dog/cat/;s/brown/green/' t.txt #-i 保存到原文件 -s执行多条命令,此处为混合使用
# cat t.txt
1 the quick green fox jumps over the lazy cat.
可以在使用-i时,后面加上 -i.bak 在修改前先备份原文件为i.bak
sed -i.bak -e 's/dog/cat/;s/brown/green/' t.txt
此时命令执行结束后,会在当前目录生成一个文件名.bak的文件备份
9.使用管道符
# echo "tom is cool" | sed 's/tom/life/'
life is cool
10.小技巧
# sed -n '$=' t.txt 统计行数
5
# sed '=' t.txt 给每行加行号
1
1 the quick green fox jumps over the lazy cat.
2
2 the quick green fox jumps over the lazy cat.
3
3 the quick green fox jumps over the lazy cat.
4
4 the quick green fox jumps over the lazy cat.
5
5 the quick green fox jumps over the lazy cat.
# sed -n -r '/^(root)(.*)(bash)$/p' /etc/passwd #使用正则表达式 -r ,显示passwd文件中root开头 bash结尾的信息
root:x:0:0:root:/root:/bin/bash
案例一:DNS监测WEB服务状态,并根据其状态实现高可用解析,场景:通过DNS进行单域名多条A记录解析做负载均衡。
#!/bin/bash
CP1=0
CP2=0
while :
do
#tong
ping -c1 192.168.18.240 > /dev/null
if [ $? -eq 1 ] && [ $CP1 -eq 0 ]
then
sed -i '/192.168.18.240/s/^/;/' /var/named/baidu.zone
/etc/init.d/named reload
CP1=1
fi
#butong
ping -c1 192.168.18.240 > /dev/null
if [ $? -eq 0 ] && [ $CP1 -eq 1 ]
then
sed -i '/192.168.18.240/s/;//' /var/named/baidu.zone
/etc/init.d/named reload
CP1=0
fi
ping -c1 192.168.18.241 > /dev/null
if [ $? -eq 1 ] && [ $CP2 -eq 0 ]
then
sed -i '/192.168.18.241/s/^/;/' /var/named/baidu.zone
/etc/init.d/named reload
CP2=1
fi
ping -c1 192.168.18.241 > /dev/null
if [ $? -eq 0 ] && [ $CP2 -eq 1 ]
then
sed -i '/192.168.18.241/s/;//' /var/named/baidu.zone
/etc/init.d/named reload
CP2=0
fi
sleep 5
done
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)