【问题标题】:powershell get just hours as output from logfilepowershell 从日志文件中获取几个小时的输出
【发布时间】:2022-12-22 20:35:12
【问题描述】:

我得到了日志文件。我的任务是找到包含“ms 2xx”、“ms 3xx”、“ms 4xx”、“ms 5xx”的行。 (HTTP 响应代码)并获得说明这些响应发生的时间的输出。

这段代码:

$logFile = "C:\Users\krivosik\Desktop\Scripts\logs\PosybeRestEPService\PosybeRestEPService20221020.log"
$response = '(?<=\d.*?ms )(2|3|4|5)\d+'
# 2022.10.20 00:12:02.122

# Read the log file into memory
$logLines = Get-Content $logFile

$filteredLines = $logLines | Where-Object { $_ -match $response }
$filteredLines

给出这个输出(部分如果它ofc):

2022.10.20 18:42:01.746 | INFO | Request finished in 3.9683ms 200 application/json; charset=utf-8
2022.10.20 18:57:01.751 | INFO | Request finished in 3.8006ms 200 application/json; charset=utf-8
2022.10.20 19:10:32.105 | INFO | Request finished in 17.5819ms 202
2022.10.20 19:11:56.322 | INFO | Request finished in 31.9491ms 202
2022.10.20 19:12:01.794 | INFO | Request finished in 3.7895ms 200 application/json; charset=utf-8
2022.10.20 19:12:53.158 | INFO | Request finished in 11.0026ms 202
2022.10.20 19:13:44.859 | INFO | Request finished in 41.7799ms 202

我应该得到这样的东西(从代码中):

18 ms200
18 ms200
19 ms202
19 ms202
19 ms200
19 ms202
19 ms202

我试过了,但它不会有所有文件的输出,但只给示例 02 一个小时,仅此而已。 :

$logFile = "C:\Users\krivosik\Desktop\Scripts\logs\PosybeRestEPService\PosybeRestEPService20221020.log"
$response = '(?<=\d.*?ms )(2|3|4|5)\d+'
# 2022.10.20 00:12:02.122

# Read the log file into memory
$logLines = Get-Content $logFile

$filteredLines = $logLines | Where-Object { $_ -match $response }
$filteredLines
$part=$filteredLines -split "\:"
$secondpart=$part[0].Trim()
$secondpart

【问题讨论】:

    标签: powershell parsing filter logfile


    【解决方案1】:

    你最好先拆分列:

    $LogLines |ForEach-Object {
        $Split = $_.Split('|')
        $Hour = (Get-Date $Split[0]).Hour
        $ms   = ($Split[2] |Select-String 'ms ddd').Matches.Value
        Write-Host $Hour $ms
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-22
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 2023-02-22
      • 2016-11-09
      • 1970-01-01
      相关资源
      最近更新 更多