三剑客之sed

三剑客之sed

注意sed和awk使用单引号,双引号有特殊解释

sed是stream editor(字符交流编辑器)的缩写,简称流编辑器。

sed是操作、过滤和转换文本内容的强大的工具。

常用功能包括结合正则表达式对文件实现快速增删改查,其中查询的功能中最常用的两大功能是过滤(过滤指定字符串)、取行(取出指定行)。

image-20220823170753399

语法:

sed [选项] [sed内置命令字符] [输入文件]

选项:

参数选项 解释
-n 取消默认sed的输出,常与sed内置命令p一起用
-i 直接将修改结果写入文件,不用-i,sed修改的是内存数据
-e 多次编辑,不需要管道符
-r 支持正则扩展

sed的 内置命令字符 用于对文件进行不同的操作功能,如对文件增删改查

sed常用,内置命令字符:

sed的内置命令字符 解释
a append,对文本追加,在指定行后面添加一行/多行文本
d Delete,删除匹配行
i insert,表示插入文本,在指定行前添加一行、多行文本
p Print,打印匹配行的内容,通常p与-n一起用
s/正则/替换内容/g 匹配正则内容,然后替换内容(支持正则),结尾g代表全局匹配

sed匹配范围

范围 解释
空地址 全文处理
单地址 指定文件某一行
/pattern/ 被模式匹配道的每一行
范围区间 10,20十到二十行,10,+5第十行向下5行,/pattern1/,/pattern2/
步长 12,表示1、3、5、7、9行,22两个步长,表示2、4、6、8、10、偶数行

sed案例

准备测试数据

[root@xuexi3 home]# touch cssed.txt
[root@xuexi3 home]# vim cssed.txt 
[root@xuexi3 home]# cat -n cssed.txt 
     1	My name is maitekai.
     2	I teach huoying.
     3	I like play computer game.
     4	My qq is 12357800
     5	My website is http://pythonav.cn
[root@xuexi3 home]# 

1.输出文件第2,3的内容

[root@xuexi3 home]# sed "2,3p" cssed.txt 
My name is maitekai.
I teach huoying.
I teach huoying.
I like play computer game.
I like play computer game.
My qq is 12357800
My website is http://pythonav.cn
[root@xuexi3 home]# sed "2,3p" cssed.txt -n  #P与-N一起使用
I teach huoying.
I like play computer game.

2.过滤含有linux的行

[root@xuexi3 home]# sed "/linux/p"  cssed.txt  -n
I teach linux.
[root@xuexi3 home]# 

3删除含有game的行

[root@xuexi3 home]# sed "/game/d" cssed.txt  #sed修改的是内存的数据
My name is maitekai.
I teach linux.
My qq is 12357800
My website is http://pythonav.cn
[root@xuexi3 home]# cat cssed.txt  #实际文件内容不会修改
My name is maitekai.
I teach linux.
I like play computer game.
My qq is 12357800
My website is http://pythonav.cn
[root@xuexi3 home]# sed "/game/d" cssed.txt -i   # -i参数将结果写入文件
[root@xuexi3 home]# cat cssed.txt 
My name is maitekai.
I teach linux.
My qq is 12357800
My website is http://pythonav.cn

4 sed删除第五行以后的数据并输出到文件

[root@xuexi3 home]# sed "5,$ d" cssed.txt -i
[root@xuexi3 home]# cat cssed.txt 
My name is maitekai.
I teach linux.
My qq is 12357800
My website is http://pythonav.cn

5将文件中的My全部替换为His

[root@xuexi3 home]# sed "s/My/His/g" cssed.txt 
His name is maitekai.
I teach linux.
His qq is 12357800
His website is http://pythonav.cn

6将文件中的His替换成My 并将QQ替换成88888888

[root@xuexi3 home]# sed "s/His/My/g" -e "s/12357800/88888888/g"  cssed.txt  #使用-e 参数多次编辑
sed: can't read s/His/My/g: No such file or directory
My name is maitekai.
I teach linux.
My qq is 88888888
My website is http://pythonav.cn

7在第二行追加内容a字符功能,写入到文件,还得添加-i

[root@xuexi3 home]# sed "2a My linux is good." cssed.txt #在添加的内容里加\n加内容可以添加两行内容
My name is maitekai.
I teach linux.
My linux is good.
My qq is 88888888
My website is http://pythonav.cn
[root@xuexi3 home]# sed "2a My linux is good." cssed.txt  -i
[root@xuexi3 home]# cat cssed.txt 
My name is maitekai.
I teach linux.
My linux is good.
My qq is 88888888
My website is http://pythonav.cn


8在每一行下面添加一行分隔符

[root@xuexi3 home]# sed "a ------" cssed.txt 
My name is maitekai.
------
I teach linux.
------
My linux is good.
------
My qq is 88888888
------
My website is http://pythonav.cn
------

linux 系统中筛选出 IP 地址

[root@xuexi3 ~]# ifconfig #第一步查看
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.102  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::c8ef:f7f6:2949:b8fb  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:72:4f:0f  txqueuelen 1000  (Ethernet)
        RX packets 6078  bytes 528181 (515.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3901  bytes 428504 (418.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 32  bytes 2592 (2.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 32  bytes 2592 (2.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:11:5c:37  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@xuexi3 ~]# ifconfig ens33 # 查看网卡地址
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.102  netmask 255.255.255.0  broadcast 10.0.0.255
        inet6 fe80::c8ef:f7f6:2949:b8fb  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:72:4f:0f  txqueuelen 1000  (Ethernet)
        RX packets 6167  bytes 534925 (522.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3944  bytes 433686 (423.5 KiB)
[root@xuexi3 ~]# ifconfig ens33 | sed -e "2s/^.*inet//" -n  -e "2s/net.*$//p" -n #用-e多次编辑加管道符输出
 10.0.0.102  

posted @ 2022-08-24 17:32  一直前进  阅读(55)  评论(0编辑  收藏  举报