将接口列表从show vlan列为全名列表按行行
我需要从Show Vlan中获取接口列表:
gi1/0/1, gi1/0/2, gi1/0/3, gi1/0/5, gi1/0/7, gi1/0/10
gi1/0/11, gi1/0/13, gi1/0/15, gi1/0/17, gi1/0/19
中获取文本输出
interface gi 1/0/1
...
interface gi 1/0/19
并在使用以下代码将原始输入作为列表
cisco1 ="""gi1/0/1, gi1/0/2, gi1/0/3, gi1/0/5, gi1/0/7, gi1/0/10
gi1/0/11, gi1/0/13, gi1/0/15, gi1/0/17, gi1/0/19
gi1/0/21, gi1/0/31, gi1/0/32, gi1/0/33, gi1/0/34, gi1/0/35, gi1/0/40"""
list1 = cisco1.split()
print(list1)
:这为我提供了一个带有额外逗号的列表(并非每次)。
['gi1/0/1,', 'gi1/0/2,', 'gi1/0/3,', 'gi1/0/5,', 'gi1/0/7,', 'gi1/0/10', 'gi1/0/11,', 'gi1/0/13,', 'gi1/0/15,', 'gi1/0/17,', 'gi1/0/19', 'gi1/0/21,', 'gi1/0/31,', 'gi1/0/32,', 'gi1/0/33,', 'gi1/0/34,', 'gi1/0/35,', 'gi1/0/40']
我想记录每个记录并删除逗号(如果在那里),然后用“ int gi”替换“ gi” 对python的狂欢的任何帮助将不胜感激。
更新: 我想出了这个代码,但我仍然需要在单独的行中打印每个int,然后在“ gi”前添加“ int”。
I need to take list of interfaces from show vlan:
gi1/0/1, gi1/0/2, gi1/0/3, gi1/0/5, gi1/0/7, gi1/0/10
gi1/0/11, gi1/0/13, gi1/0/15, gi1/0/17, gi1/0/19
and get the text output as
interface gi 1/0/1
...
interface gi 1/0/19
I got my original input as a list using following code:
cisco1 ="""gi1/0/1, gi1/0/2, gi1/0/3, gi1/0/5, gi1/0/7, gi1/0/10
gi1/0/11, gi1/0/13, gi1/0/15, gi1/0/17, gi1/0/19
gi1/0/21, gi1/0/31, gi1/0/32, gi1/0/33, gi1/0/34, gi1/0/35, gi1/0/40"""
list1 = cisco1.split()
print(list1)
This gives me a list with extra commas (not every time).
['gi1/0/1,', 'gi1/0/2,', 'gi1/0/3,', 'gi1/0/5,', 'gi1/0/7,', 'gi1/0/10', 'gi1/0/11,', 'gi1/0/13,', 'gi1/0/15,', 'gi1/0/17,', 'gi1/0/19', 'gi1/0/21,', 'gi1/0/31,', 'gi1/0/32,', 'gi1/0/33,', 'gi1/0/34,', 'gi1/0/35,', 'gi1/0/40']
I would like to take each record and remove comma (if its there) and replace "gi" with "int gi"
Any help in bash of python would be appreciated.
Update:
I came up with this code but i still need to print each int in separate line and add "int " in front of "gi".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的字符串中有多个定界符。
使用正则表达式。
要替换,您可以使用列表理解。
You have multiple delimiters in your string.
use regular expression.
for replacing you can use list comprehension.
谢谢帕文。这对我有很大帮助。
这是最终代码:
最终输出行是:
Thank you Pavan. This helped me a lot.
Here is final code:
final lines of output are: