首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用linux或python设置文本格式

使用linux或python设置文本格式
EN

Stack Overflow用户
提问于 2018-07-10 00:48:50
回答 1查看 59关注 0票数 0

我是个脚本新手,所以任何帮助都会有很大的帮助。

它更多地涉及到Cisco路由器配置文件,第一种格式是当您运行命令"show running- config“时,第二部分是当您使用命令"show running-config formal”时

因此,我想将我的配置txt文件转换为稍后的格式。

我在linux操作系统中有一个文件,它缩进了以下数据,

代码语言:javascript
复制
control-plane     
 management-plane   
  inband    
   interface all   
    allow xy   
     address  x.x.x.x   
     address  y.y.y.y   

    !   
    allow b   
     address  b.b.b.b   
    !  
   !  
  !  

我想要的是删除它的格式,这样基本上就是像这样将子部分附加到父文件中,使用linux命令或使用python编写脚本。你认为最好的方法是什么?

代码语言:javascript
复制
control-plane management-plane inband interface all allow xy   
control-plane management-plane inband interface all allow xy address  x.x.x.x   
control-plane management-plane inband interface all allow xy address  y.y.y.y   
control-plane management-plane inband interface all allow b   
control-plane management-plane inband interface all allow b address b.b.b.b

使用相同的概念,我想在相同的文件中进一步重新格式化,

代码语言:javascript
复制
service sesh instance1
 service-location preferred-active 0/3/CPU0
 service-type nps nps-1
  forced-placement npu 0
  tunnel type gre
   name gre10
   tunnel-destination ipv4 address 209.165.200.225
   ipv4 address 192.0.2.6/24
   remote ipv4 address 192.0.2.5/24
   tunnel-source ipv4 address 209.165.200.226
  !
  package nps-mips64-r2.rpm
  interface ServiceApp1
   remote ipv4 address 209.165.200.227/24
  !
 !
!

template pre-pos
 interface preconfigure POS0/1/0/0
  ipv4 address 10.3.32.154 255.0.0.0 
!

我还附上了an image,只是为了给你一个更清晰的图像。

谢谢

销售

EN

回答 1

Stack Overflow用户

发布于 2018-07-10 01:45:30

希望这能有所帮助:

代码语言:javascript
复制
file = open('text.txt','r')
allow_indices=[]
exl_indices=[]
content=[]
i = 0
for line in file:
    if 'allow' in line:
        allow_indices.append(i)
    if '!' in line:
        exl_indices.append(i)
    content.append(line)
    i += 1
file = open('text.txt','r')

base=''
formatted=''

for i in range(0,allow_indices[0]):
    base+= content[i]

close_index=0
for i in allow_indices:
    new_base = base+content[i]
    formatted+=new_base.replace('\n','')+'\n'
    for j in range(i+1,exl_indices[close_index]-1):
        formatted+=new_base.replace('\n','')+content[j].replace('\n','')+'\n'
        close_index+=1

print formatted

我使用以下内容作为text.txt文件的内容:

代码语言:javascript
复制
control-plane
 management-plane
  inband
   interface all
    allow xy
     address x.x.x.x
     address y.y.y.y

    !
    allow b
     address  b.b.b.b
    !
   !
  !

我得到了以下结果:

代码语言:javascript
复制
control-plane      management-plane     inband       interface all       allow xy   
control-plane      management-plane     inband       interface all       allow xy        address x.x.x.x   
control-plane      management-plane     inband       interface all       allow xy        address y.y.y.y   
control-plane      management-plane     inband       interface all       allow b   
control-plane      management-plane     inband       interface all       allow b        address  b.b.b.b 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51250443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档