Powershell在特定行后查找字符串
我有一个日志文件,它在每次重新启动后都会添加一行restart successfully。从最后一行restart successfully开始,我想从搜索词列表中查找其后包含单词的任何行 我该怎么做?我的部分工作代码如下:Powershell在特定行后查找字符串,powershell,Powershell,我有一个日志文件,它在每次重新启动后都会添加一行restart successfully。从最后一行restart successfully开始,我想从搜索词列表中查找其后包含单词的任何行 我该怎么做?我的部分工作代码如下: $contentsOfLog = Get-Content $inputFile $keyWords = $contentsOfLog | Select-String 'restart successfully' $linenumbers = $keyWords.LineN
$contentsOfLog = Get-Content $inputFile
$keyWords = $contentsOfLog | Select-String 'restart successfully'
$linenumbers = $keyWords.LineNumber
$linenumber = $linenumbers[$linenumbers.Length - 1]
$inputLines = ""
For ($i=$linenumber; $i -lt $contentsOfLog.length; $i++) {
$line = $contentsOfLog[$i]
$line = $line|select-string -pattern $error|Out-String
$inputLines += $line.TrimEnd()
}
获取行号,从第一次通过选择字符串开始,就像您已经找到的一样,然后使用“选择对象-跳过”跳过前面的行:
$contentsOfLog = Get-Content $inputFile
$startFrom = $contentsOfLog |Select-String 'restart successfully' |Select -Last 1 -Expand LineNumber
$inputLines = $contentsOfLog |Select -Skip $startFrom |Select-String $error |Select -Expand Line
非常感谢Mathias一如既往: