我有下面的列表作为例子,如何从下面的列表中grep IP,并使用robot框架库将其放在另一个列表中?这只是一个例子,根据路由表的大小,这个列表可能相当大。
${routes} = ['201', '0000.aaaa.bbbb', '10.10.1.11', 'BGP', 'L,', '2', 'Local', 'leaf1#','202', '0000.cccc.bbbb', '10.10.1.12', 'BGP', 'L,', '2', 'Local', 'leaf2#','201', '0000.dddd.bbbb', '10.10.1.21', 'BGP', 'L,', '2', 'Local', 'leaf1#']
新的列表应该像下面这样,列表中只有IP:
${routes-ip} =['10.10.1.11','10.10.1.12','10.10.1.21']
遍历大列表,检查每个元素是否为您要查找的元素之一;如果是,则将其放入";结果列表":
${target values}= Create List 10.10.1.11 10.10.1.12 10.10.1.21
${a very long list with all values}= Keyword That Returns The List, Or Some Other Way You Get Them
${found values}= Create List # an empty one, will hold all values in the long that were in the ${target values}
FOR ${value} IN @{a very long list with all values}
${this is a target value}= Run Keyword And Return Status List Should Contain Value ${target values} ${value}
Run Keyword If ${this is a target value} Append To List ${found values} ${value}
END
Log To Console All target values found in the long list: ${found values}
您可以尝试使用Regex获取Ip的
例如
Create IP Address List
@{routes} Create List
... 201 0000.aaaa.bbbb 10.10.1.11 BGP L, 2 Local leaf1#
... 202 0000.cccc.bbbb 10.10.1.12 BGP L, 2 Local leaf2#
... 201 0000.dddd.bbbb 10.10.1.21 BGP L, 2 Local leaf1#
@{routes-ip} Create List
FOR ${element} IN @{routes}
${status} Run Keyword And Return Status Should Match Regexp ${element} \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Run Keyword If "${status}"=='True' Append To List ${routes-ip} ${element}
END
Log To Console ${routes-ip}