1、将字段串“plugins=1”替换为“plugins=0”
sed -i 's/plugins=1/plugins=0/g' /etc/yum.conf
2、将字符串“\$1>="\$date_source" \&\& \$1<="\$date_dest"”替换为“\$1>='\"\$date_source\"' \&\& \$1<='\"\$date_dest\"'”
sed -i 's/\$1>="\$date_source" \&\& \$1<="\$date_dest"/\$1>='\"\$date_source\"' \&\& \$1<='\"\$date_dest\"'/g' check_sarlog.sh 注意: "\$1":加\反编译特殊字符"$"; "\&\&":加\反编译特殊字符字符"&"; "\$date_dest":加\反编译特殊字符字符"$";
3、删除关键字"111",避免存在空行导致shell脚本或者配置config执行报错
sed -i "/111/d" /tmp/text.txt [root@Harbor ~]# cat /tmp/text.txt 111 222 333 [root@Harbor ~]# sed -i "/111/d" /tmp/text.txt [root@Harbor ~]# cat /tmp/text.txt 222 333
4、打印关键字“DNS”行
[root@Harbor ~]# sed -n "/DNS/p" /etc/ssh/sshd_config #UseDNS no
5、删除文件text.txt第二行"222 two"并打印
[root@Harbor tmp]# cat text.txt 111 one 222 two 333 three [root@Harbor tmp]# sed '2d' text.txt 111 one 333 three
6、删除文件text.txt多行并打印
# 删除第一行到第二行
[root@Harbor tmp]# sed '1,2d' text.txt 333 three
# 删除第二行到第三行 [root@Harbor tmp]# sed '2,3d' text.txt 111 one
7、删除[Tt]大小写wo的字符并打印
[root@Harbor tmp]# sed '/[Tt]wo/d' text.txt 111 one 333 three
8、打印指定3,4行
[root@Harbor tmp]# sed -n "3,4p" text.txt 333 three 444 Two
9、打印第3到第4行将字符Two和two替换为Ten
[root@Harbor tmp]# sed '1,4s/[Tt]wo/Ten/g' text.txt 111 one 222 Ten 333 three 444 Ten
10、删除不包含字符Two和two行并打印
[root@Harbor tmp]# sed '/[Tt]wo/!d' text.txt 222 two 444 Two
11、打印包含字符Two和two的行到文件/tmp/tmp.txt
[root@Harbor tmp]# sed -n '/[Tt]wo/w /tmp/tmp.txt' text.txt [root@Harbor tmp]# cat /tmp/tmp.txt 222 two 444 Two
标签:总结,tmp,Harbor,text,sed,Linux,txt,root From: https://www.cnblogs.com/gkhost/p/18586444