0

特定の文字列 (「タイムアウト期間」) を検索しているというログがあります。以下を使用して、ログでこれのインスタンスを取得できます。

Get-Content mylog.txt | select-string 'the timeout period' | select-object

私が抱えている問題は、これが XML ファイルであり、powershell がエラーのある行だけをピックアップしているのに、実際にはデータ/時刻情報が必要なことです。必要なのは、「タイムアウト期間」に一致するライン インスタンスの直前に「スレッド」に一致するライン インスタンスです。例えば:

Thread 3423 - 6:34:00
Info following. ..... ....
Error .... the timeout period

だから私が出力したいのは:

Thread 3423 - 6:34:00
4

2 に答える 2

1

ファイルが XML の場合、Select-Xml と XPath 検索パターンの使用を検討します。そうは言っても、次のように Select-String を使用できます。

Get-Content mylog.txt | select-string "the timeout period" -context 2 | 
    Foreach {$_.Context.PreContext[0]}
于 2013-01-22T16:48:15.237 に答える
1

情報部分は複数行になる可能性があると思います。だからこれは私の試みです:

$content = Get-Content .\test.txt
$content | select-string "the timeout period" | % {
    $i = ($_.linenumber -1)

    while ($content[$i] -notlike "Thread*") {
        $i--
    }

    $content[$i]
}

エラーごとに「Thread」で始まる前の行が表示されます。元。出力:

Thread 3423 - 1:34:00
Thread 3423 - 2:34:00
Thread 3423 - 3:34:00
Thread 3423 - 4:34:00
于 2013-01-22T17:12:45.577 に答える