管线工具sed和正则表达式

丶灬走出姿态 提交于 2020-02-12 00:33:59

查看文档/etc/passwd 文档并删除2,5行

[root@localhost yaotameng]# nl /etc/passwd |sed '2,5d'
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
......
[root@localhost yaotameng]#

只删除第二行

[root@localhost yaotameng]# nl /etc/passwd |sed '2d'
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
........
[root@localhost yaotameng]#

在第二行后加[hello,word!]

[root@localhost yaotameng]# nl /etc/passwd |sed '2a hello,word!'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
hello,word!
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
........
[root@localhost yaotameng]#

在第二行前加[hello,word!]

[root@localhost yaotameng]# nl /etc/passwd |sed '2i hello,word!'
1 root:x:0:0:root:/root:/bin/bash
hello,word!
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
.........
[root@localhost yaotameng]#

 

以行为单位取代与显示

2,5行替换为[No 2-5 number]

[root@localhost yaotameng]# nl /etc/passwd |sed '2,5c No 2-5 number'
1 root:x:0:0:root:/root:/bin/bash
No 2-5 number
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
......
[root@localhost yaotameng]#

仅列出文档的5-7行

[root@localhost yaotameng]# nl /etc/passwd |sed -n '5,7p'
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost yaotameng]#

 

部分数据的搜索和取代

获取ip

[root@localhost yaotameng]# /sbin/ifconfig eth0 (获取IP信息)
eth0 Link encap:Ethernet HWaddr 00:0C:29:DE:1C:BA
inet addr:192.168.136.143 Bcast:192.168.136.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fede:1cba/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:5477 errors:0 dropped:0 overruns:0 frame:0
TX packets:4000 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:626625 (611.9 KiB) TX bytes:447925 (437.4 KiB)

[root@localhost yaotameng]#

获取inet addr行

[root@localhost yaotameng]# /sbin/ifconfig eth0 |grep 'inet addr:'
inet addr:192.168.136.143 Bcast:192.168.136.255 Mask:255.255.255.0
[root@localhost yaotameng]#

删除inet addr:

[root@localhost yaotameng]# /sbin/ifconfig eth0 |grep 'inet addr:'|sed 's/.*addr://g' ( g获得内存缓冲区的内容,并替代当前模板块中的文本。因为为空,所以=删除)
192.168.136.143 Bcast:192.168.136.255 Mask:255.255.255.0
[root@localhost yaotameng]# /sbin/ifconfig eth0 |grep 'inet addr:'|sed 's/^.*addr://g' (^表示inet addr:在行首)
192.168.136.143 Bcast:192.168.136.255 Mask:255.255.255.0

删除Bcast:到行尾的数据 

[root@localhost yaotameng]# /sbin/ifconfig eth0 |grep 'inet addr:'|sed 's/^.*addr://g'|sed 's/Bcast:.*$//g'
192.168.136.143

 

 

 

 

 

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!