sed

sed

descriptions

  1. 对文件进行增、删、改、查

替换

语法:sed 选项 's/搜索的内容/替换的内容/动作' 需要处理的文件

其中,s表示search搜索;斜杠/表示分隔符,可以自己定义;动作一般是打印p和全局替换g

options

参数 参数说明
-i --in-place,替换,使用\n时,-i后面需使用\进行转义
-r --regexp-extended扩展正则表达式
-n --quiet, --silent,不打印,静默替换,默认模式
\\n \反斜线转义
-e 前面使用| 管道后,对前面内容输出进行sed,使用\n时,-e后面无需使用\进行转义,使用[]时,需要使用\进行转义

examples

sed '' ip.txt

[root@rhel tmp]# sed '' ip.txt
192.168.1.1
192.168.1.2
192.168.1.3

sed -i s/192/172/g ip.txt

[root@rhel tmp]# sed '' ip.txt
192.168.1.1
192.168.1.2
192.168.1.3
[root@rhel tmp]# sed -i s/192/172/g ip.txt
[root@rhel tmp]# sed '' ip.txt
172.168.1.1
172.168.1.2
172.168.1.3

sed -i -r s/n/\\n/g ip

[root@rhel tmp]# cat ip
192.168.3.1n
192.168.3.2n
192.168.3.3n
192.168.3.4
192.168.3.5
192.168.3.6

[root@rhel tmp]# sed -i -r s/n/\\n/g ip

[root@rhel tmp]# cat ip
192.168.3.1

192.168.3.2

192.168.3.3

192.168.3.4
192.168.3.5
192.168.3.6

N7K与华为CE168路由表对比

[root@rhel 1]# more routing-02 | head -10
10.4.3.21/32, ubest/mbest: 2/0, attached
    *via 10.4.3.21, Lo0, [0/0], 1y38w, local
    *via 10.4.3.21, Lo0, [0/0], 1y38w, direct
10.224.3.132/32, ubest/mbest: 2/0
    *via 10.4.132.130, Vlan1001, [110/32], 29w0d, ospf-65010, intra
    *via 10.4.132.162, Vlan170, [110/32], 21w2d, ospf-65010, intra
9.8.0.0/16, ubest/mbest: 1/0
    *via 10.4.132.13, [6/0], 22w5d, bgp-65010, external, tag 65000
9.24.15.0/24, ubest/mbest: 1/0
    *via 10.4.132.13, [6/0], 13w5d, bgp-65010, external, tag 65000

-------1.得到网络号、掩码字段-----------
[root@rhel 1]# more routing-02 | grep ubest/mbest | sed -r -e ' s/,.*1\/0//' -e ' s/,.*2\/0.*/\n/'
10.4.3.21/32

10.224.3.132/32

9.8.0.0/16
9.24.15.0/24
实现效果:
1.将, ubest/mbest: 1/0替换为空
2.将, ubest/mbest: 2/0替换为\n(换行)

--------2.得到下一跳字段--------------
[root@rhel 1]# more routing-02 | grep via | sed -e ' s/.*via //' -e ' s/,.*//'
10.4.3.21
10.4.3.21
10.4.132.130
10.4.132.162
10.4.132.13
10.4.132.13

---------3.得到路由优先级字段-------------------------
[root@rhel 1]# more routing-02 | grep via | sed -e ' s/.*\[//' -e ' s/\/.*//'
0
0
110
110
6
6

----------4.得到cost字段--------------------------

[root@rhel 1]# more routing-02 | grep via | sed -e ' s/.*\[//' -e ' s/\].*//' | awk -F "/" '{print $2}'
0
0
32
32
0
0


more routing-01 | grep ubest/mbest | sed -e ' s/,.*1\/0//' -e ' s/, attached//' -e ' s/,.*2\/0.*/\n/' > network_mask
more routing-01 | grep via | sed -e ' s/.*via //' -e ' s/,.*//' >next_hop
more routing-01 | grep via | sed -e ' s/.*\[//' -e ' s/\/.*//'  >routing_priority
more routing-01 | grep via | sed -e ' s/.*\[//' -e ' s/\].*//' | awk -F "/" '{print $2}' >routing_cost
posted @ 2023-03-16 22:20  wefjack  阅读(43)  评论(0)    收藏  举报