麒麟操作系统基础知识保姆级教程(十一)三剑客之sed

如果你想拥有你从未拥有过的东西,那么你必须去做你从未做过的事情

今天开始对sed的学习,sed是三剑客中的老二,sed的功能非常强大,可以按行进行过滤,也可以用来进行文件的内容替换,但同时也会带来不小的资源占用。

目录

sed的功能和语法

sed的优势和劣势

sed案例

案例1、按指定行查找

案例2、模糊过滤文件内容

案例3、sed删除

案例4、sed替换

案例5、sed追加插入替换

案例6、sed向后引用

sed小结


💬欢迎交流:在学习过程中如果你有任何疑问或想法,欢迎在评论区留言,我们可以共同探讨学习的内容。你的支持是我持续创作的动力!

👍点赞、收藏与推荐:如果你觉得这篇文章对你有所帮助,请不要忘记点赞、收藏,并分享给更多的小伙伴!你们的鼓励是我不断进步的源泉!

🚀推广给更多人:如果你认为这篇文章对你有帮助,欢迎分享给更多对Linux感兴趣的朋友,让我们一起进步,共同提升!

sed的功能和语法

1.模糊过滤文件的内容
2.查找指定的行
3.对文件进行增删改查
4.替换
5.格式化输出内容,后向引用
sed语法格式: 
            sed '/过滤内容/' 文件
            sed '模式 动作' 文件
            sed '找谁 干啥' 文件

sed的优势和劣势

优势
    强大的文本编辑能力:sed 是一个流编辑器,可以对文本进行多种编辑操作,如替换、删除、插入等。
    灵活的行处理:可以基于行号或者模式匹配来处理特定的行。
    支持脚本化操作:可以将一系列的 sed 命令组合成脚本,用于处理复杂的文本编辑任务。
​
劣势
    学习曲线较陡:sed 的命令语法相对复杂,尤其是对于初学者来说,需要花费一些时间来理解和掌握诸如替换、插入、删除等操作的语法规则。
    处理大型文件效率可能受影响:在处理非常大型的文件时,如果进行复杂的编辑操作,sed 的性能可能会下降。

sed案例

案例1、按指定行查找

语法格式:   sed -n '3p'文件  #print 打印输出
            cat 文件|sed -n '3p'
显示文件的第3行:
创建测试环境
[root@yunzhongzi yunzhongzi]# head -5 /etc/passwd > test.txt
[root@yunzhongzi yunzhongzi]# sed -n '3p' test.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
显示文件最后一行:
[root@yunzhongzi yunzhongzi]#  sed -n '$p' test.txt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
过滤区间范围:sed -n '2,4p'  文件
过滤文件的2-4行
[root@yunzhongzi yunzhongzi]# sed -n '2,4p' test.txt
显示文件第二行到最后一行
[root@yunzhongzi yunzhongzi]# sed -n '2,$p' test.txt
处理屏幕上显示的内容:
[root@yunzhongzi ~]# ifconfig eth0|sed -n '2p'
        inet 10.0.0.200  netmask 255.255.255.0  broadcast 10.0.0.255

案例2、模糊过滤文件内容

语法结构        sed -n '/过滤的内容/p' file
[root@yunzhongzi yunzhongzi]#  sed -n '/root/p' test.txt
root:x:0:0:root:/root:/bin/bash
使用正则:
[root@yunzhongzi yunzhongzi]# sed -n '/^r/p' test.txt
root:x:0:0:root:/root:/bin/bash
过滤以a或b或c开头的行
[root@yunzhongzi yunzhongzi]# sed -n '/^[a-c]/p' test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
过滤以什么结尾的行
[root@yunzhongzi yunzhongzi]# sed -n '/h$/p' test.txt
root:x:0:0:root:/root:/bin/bash
过滤区间范围 
            sed -n '//,//p'  文件
            sed -n '2,4p'    文件
过滤字符串区间范围
[root@yunzhongzi yunzhongzi]# sed -n '/^bin/,/^a/p' test.txt
[root@linuxnc ~]# sed -n '/2024:08:00:15/,/2024:09:30:42/p' /var/log/nginx/access.log
注意匹配方式:
 [root@db01 ~]# cat a.txt
 aaa
 bbb
 ccc
 bbb
 uuu
 ddd
 eee
 fff
 ddd
 xxx
 [root@db01 ~]# sed -n '/bbb/,/ddd/p' a.txt
 bbb
 ccc
 bbb
 uuu
 ddd

案例3、sed删除

指定行删除:
语法格式: sed '3d' file  #默认不会修改源文件
         sed -i '3d' file
案例1)删除文件第三行
[root@yunzhongzi yunzhongzi]# sed '3d' test.txt
案例2)删除文件2-4行
[root@yunzhongzi yunzhongzi]# sed '2,4d' test.txt
案例3)删除2到最后一行
[root@yunzhongzi yunzhongzi]# sed '2,$d' test.txt
root:x:0:0:root:/root:/bin/bash
模糊过滤然后删除
语法结构:sed '/字符串/d' 文件
[root@yunzhongzi yunzhongzi]# sed '/root/d' test.txt
删除以d开头的行
[root@yunzhongzi yunzhongzi]# sed '/^b/d' test.txt
-r 支持扩展正则 |或者的意思
[root@yunzhongzi yunzhongzi]# sed -rn '/^bin|^lp/p' test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
删除bin开头或者以lp开头的行
[root@yunzhongzi yunzhongzi]# sed -r '/^bin|^lp/p' test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

案例4、sed替换

语法结构:
        sed 's#被替换#替换成#g'文件
        sed 's@@@g'文件
        sed 's///g'文件
        
        sed -i 's###g' file 可以将替换的内容保存到文件中(生产环境中不建议这么搞,会出大事的)
        
案例:只替换每行的第一个单词 root为yunzhongzi
[root@yunzhongzi yunzhongzi]# sed 's#root#qy#g' test.txt
案例:替换所有的root为qy
[root@yunzhongzi yunzhongzi]# sed 's#root#qy#g' test.txt
案例:将root替换成空
[root@yunzhongzi yunzhongzi]# sed 's#root##g' test.txt
案例:删除所有数字
[root@yunzhongzi yunzhongzi]# sed 's#[0-9]##g' test.txt
案例:删除所有字符串
[root@yunzhongzi yunzhongzi]# sed 's#[a-z]##g' test.txt
案例:统计passwd中的每个单词出现的次数 top10
[root@yunzhongzi yunzhongzi]# sed 's#[:/0-9x]# #g' test.txt|xargs -n1|sort|uniq -c|sort -rn|head
边界符
[root@yunzhongzi yunzhongzi]# sed 's#\bbin\b#yunzhongzi#g' test.txt
或者
[root@yunzhongzi yunzhongzi]# sed 's#\<bin\>#yunzhongzi#g' test.txt
注意:若有语法冲突,换个语法或使用撬棍\
[root@db01 ~]# sed 's/\/root/\/yunzhongzi/g' test.txt
或者
[root@db01 ~]# sed 's#/root#/yunzhongzi#g' test.txt
动作:
    sed -n '3p'文件
    sed 's###g'文件
模式+动作
模式:找谁,如何找
sed  -n '3p'  #找第3行
案例:将第三行的sbin替换为yunzhongzi
[root@yunzhongzi yunzhongzi]# sed '3s#sbin#yunzhongzi#g' test.txt
案例:将2-4行的sbin替换为yunzhongzi
[root@yunzhongzi yunzhongzi]# sed '2,4s#sbin#yunzhongzi#g' test.txt
筛选有adm的行将adm替换为yunzhongzi
[root@yunzhongzi yunzhongzi]# sed '/adm/s#adm#yunzhongzi#g' test.txt
区间范围:
[root@yunzhongzi yunzhongzi]# sed '/adm/,/lp/s#[a-z]##g' test.txt

案例5、sed追加插入替换

1.i插入 在第3行写入aaa(第三行向后顺移)
[root@yunzhongzi yunzhongzi]# sed '3i aaa' test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
aaa
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
2.a追加,在第3行的后一行,第四行写入aaa(原第四行向后顺移)
[root@yunzhongzi yunzhongzi]# sed '3a aaa' test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
aaa
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
3.c替换 替换整行内容(替换第i3行)
[root@yunzhongzi yunzhongzi]# sed '3c aaa' test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
aaa
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
4.w保存 将找到的行替换到新的文件中
[root@yunzhongzi yunzhongzi]# cat new.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

案例6、sed向后引用

语法结构:sed s#()()()#\1\2\3#
案例:取出IP地址
[root@yunzhongzi ~]# ifconfig eth0|sed -n '2p'|sed -r 's#^.*t(.*) netm.*$#\1#g'
 10.0.0.200 
2.命令拼接后交给bash执行
[root@yunzhongzi ~]# seq 5|sed -r 's#(.*)#touch \1.txt#g'|bash
[root@yunzhongzi ~]# ll
3.批量创建用户
[root@yunzhongzi ~]# echo test{1..3}
test1 test2 test3
[root@yunzhongzi ~]# echo test{1..3}|xargs -n1
test1
test2
test3
[root@yunzhongzi ~]# echo test{1..3}|xargs -n1|sed -r 's#(.*)#\1#g'
test1
test2
test3
[root@yunzhongzi ~]# echo test{1..3}|xargs -n1|sed -r 's#(.*)#useradd\1#g'
useraddtest1
useraddtest2
useraddtest3
[root@yunzhongzi ~]# echo test{1..3}|xargs -n1|sed -r 's#(.*)#useradd \1#g'
useradd test1
useradd test2
useradd test3
[root@yunzhongzi ~]# 

sed小结

1.指定行查找
  sed -n '3p'   file
  sed -n '3,6p' file 
2.模糊过滤
  sed -n '/过滤的内容/p' file
  sed -n '//,//p' file
  正则:
  sed -n '/^a/p' file
  sed -n '/n$/p' file
  sed -rn '/^a|h$/' file
 3.删除
  sed '3d'   file 
  sed '3,6d' file
  模糊过滤后删除
  sed '/root/d' file
  sed '//,//d'  file
 4.替换
  sed 's/将谁/替换成谁/g' file
  sed 's###g'
  注意: 如果涉及到变量需要使用双引号
模式+动作
  sed '3s#bin##g' file  # 将第3行的bin替换成空,删除的意思
  sed '3,10s###g' file
  sed '/root/s#bin#yunzhongzi#g' file
其他参数:
   sed '3a  aaa'
   sed '3i  aaa'
   sed '3c  aaa'
   sed '3w  test.txt'
   sed -r 's#()#\1#g' file

好了,今天的分享就到这里了,下一篇我准备分享三剑客大哥awk


想成为大佬,就要从小白开始,从0开始,一点一点的积累,慢慢成长,明天你就是大佬!!想学习更多麒麟操作系统的知识,关注小屁,让你成为运维老鸟~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值