我被困在代码中,我的要求是获取两个字符串(变量)之间的行,请提出一个正则表达式模式
输入:
Device Name: test
Status: Completed
Results:
[PUSH_TERMLET] Action completed successfully...
Pushing configuration file 'PUSH_TERMLET' for IDX 60504 with mechanism SSH
****** Enable Mode Results ******
sh run | sec ip access-list standard customer_internal
ip access-list standard customer_internal
permit 204.79.49.116
permit 159.12.130.0 0.0.0.31
permit 10.108.30.0 0.0.0.255
permit 10.108.141.0 0.0.0.127
permit 10.108.172.0 0.0.0.255
permit 10.108.160.0 0.0.0.31
test#
输出:
permit 204.79.49.116
permit 159.12.130.0 0.0.0.31
permit 10.108.30.0 0.0.0.255
permit 10.108.141.0 0.0.0.127
permit 10.108.172.0 0.0.0.255
permit 10.108.160.0 0.0.0.31
这是我正在尝试的代码
$content = Get-Content -Path "TestACL.txt"
$device_list = $content | Select-String -Pattern "device name" -AllMatches
foreach($temp in $device_list) {
$temp.sp
$pattern = "(?<=$temp|permit)(.*)"
$content | Select-String -Pattern $pattern
}
除此之外,我还尝试了其他模式,但没有一个对我有用。
答案 0 :(得分:0)
您的正则表达式有点接近。让我们来探索这个选项。
$Content = (Get-Content "C:\temp\TestACL.txt") -join ' '
$variable = "test#"
$Output = ($Content | Select-String -Pattern "(?:permit )(.*)(?= $($variable))").Matches.Value
(?:permit )
将找到单词permit
(含空格),?:
将其包含在比赛中。
(?= test#)
将对单词test#
赋以空格(包括空格),并且?=
将匹配但从主表达式后的结果中排除。
(.*)
将捕获第一个条件和最后一个条件之间的所有内容