Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell在特定行后查找字符串_Powershell - Fatal编程技术网

Powershell在特定行后查找字符串

Powershell在特定行后查找字符串,powershell,Powershell,我有一个日志文件,它在每次重新启动后都会添加一行restart successfully。从最后一行restart successfully开始,我想从搜索词列表中查找其后包含单词的任何行 我该怎么做?我的部分工作代码如下: $contentsOfLog = Get-Content $inputFile $keyWords = $contentsOfLog | Select-String 'restart successfully' $linenumbers = $keyWords.LineN

我有一个日志文件,它在每次重新启动后都会添加一行restart successfully。从最后一行restart successfully开始,我想从搜索词列表中查找其后包含单词的任何行

我该怎么做?我的部分工作代码如下:

$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一如既往: