Select-String用于解析Cisco配置文件



所以我试图从cisco路由器解析一个show tech配置文件。

配置文件是巨大的,所以我需要捕获两个字符串之间的一切…例如:

------------------ show running-config ------------------
everything here
------------------ show redundancy history ------------------

我已经能够通过REGEX做到这一点:

$TechFile = "filepath"
$Cisco = Get-Content $TechFile
$ShowRunningCfgPattern = "------------------ show running-config ------------------(.*?)------------------ show redundancy history ------------------"
$Results = [regex]::Match($Cisco,$ShowRunningCfgPattern).Groups[1].Value

然而,这不是一个理想的解决方案,因为它只是将所有内容聚集在一起,使得进一步解析这些信息变得不可能

有人知道如何解决这个问题吗?

您可以首先获得特定字符串的linenumber/index,然后使用select-object只选择这些范围内的行。请参阅下面的代码示例,在我的机器上测试成功:

$startSTR = "------------------ show running-config ------------------"
$endSTR = "------------------ show redundancy history ------------------"
$testfile = "C:Temptest.txt"
$content = get-content $testfile
$startIndex = $content.indexof($startSTR) + 1
$endIndex = $content.indexof($endSTR) -1
$desiredContent = $content | select-object -index ($startIndex..$endIndex)
$desiredContent

最新更新