【问题标题】:Parsing custom log files解析自定义日志文件
【发布时间】:2016-12-17 21:54:20
【问题描述】:

我有一个日志文件 (*.log) 我希望解析和查询如下:

第 33043 行:17/07/2016;13:26:45;GetMasterOrderNo;Master Order No is : 1117103907 for SoSupplierOrderNo, 1117103907
第 33048 行:17/07/2016;13:26:45;AddAutoPurchHdr;无法保存 PurchHdr 记录 - 供应商订单号已在交货单号 1117103907(订单号 1117103907)、供应商名称(51)中使用
第 33049 行:17/07/2016;13:26:45;ImportASN;ConvertASNFiles:导入 GRN1171_0000700384_1117103907.xml 失败。无法保存 PurchHdr 记录 - 供应商订单号已在交货单号 1117103907(订单号 1117103907)、供应商供应商名称(51)中使用

我想要做的是用标题分割每一行,如下所示:

  • 线,
  • 日期,
  • 时间,
  • 类型,
  • 说明

...这样我就可以对此进行查询了。

最好的方法是什么?

【问题讨论】:

  • 您有分号分隔的字段。您可以将其解析为带有分号分隔符的 CSV。
  • @ChrisDent 那是我的第一次尝试,但是 e。 G。该行没有用分号分隔,描述也可以包含分号...

标签: regex powershell powershell-2.0 logfile-analysis


【解决方案1】:

使用带有名称的正则表达式捕获组来为自定义对象制作哈希表键:

Get-Content log.txt | ForEach {
    $_ -match '^Line (?<Line>\d+): (?<Day>..)/(?<Month>..)/(?<Year>....);(?<Time>.*?);(?<Type>.*?);(?<Message>.*)$'

    # Cast date and line to useful types (optional)
    $Matches['Date'] = Get-Date ($Matches['Year']+'-'+$Matches['Month']+'-'+$Matches['Day']+' '+$Matches['Time'])
    $Matches['Line'] = [int]$Matches['Line']

    New-Object -Type PSCustomObject -Property $Matches
}

【讨论】:

    【解决方案2】:

    对 Martin 的非常好的答案进行了一些修正。 [PSCustomObject] 构造在 powershell v2 主机上不起作用。

    $content = Get-Content 'your_log_path' -raw
    $regex = 'Line\s+(\d+):\s+([^;]+);([^;]+);([^;]+);(.+)'
    [regex]::Matches($content, $regex) | ForEach-Object {
        $obj = New-Object PSObject
        $obj | Add-Member -MemberType NoteProperty -Name Line -Value $_.Groups[1].Value
        $obj | Add-Member -MemberType NoteProperty -Name Date -Value $_.Groups[2].Value
        $obj | Add-Member -MemberType NoteProperty -Name Time -Value $_.Groups[3].Value
        $obj | Add-Member -MemberType NoteProperty -Name Type -Value $_.Groups[4].Value
        $obj | Add-Member -MemberType NoteProperty -Name Description -Value $_.Groups[5].Value
        $obj
    }
    

    【讨论】:

    • 没错,我错过了这个版本! +1
    • 但是-Property hashtable 参数应该可以工作,对吧?
    • V2 是我桌面上的东西,虽然我总是可以在运行 V4 的服务器上运行它
    【解决方案3】:

    您可以使用正则表达式来捕获这些字段:

    $content = Get-Content 'your_log_path' -raw
    $regex = 'Line\s+(\d+):\s+([^;]+);([^;]+);([^;]+);(.+)'
    [regex]::Matches($content, $regex) | ForEach-Object {
        [PsCustomObject]@{
            Line = $_.Groups[1].Value
            Date = $_.Groups[2].Value
            Time = $_.Groups[3].Value
            Type = $_.Groups[4].Value
            Description = $_.Groups[5].Value
        }
    }
    

    输出:

    Line        : 33043
    Date        : 17/07/2016
    Time        : 13:26:45
    Type        : GetMasterOrderNo
    Description : Master Order No is :  1117103907 for SoSupplierOrderNo, 1117103907
    
    Line        : 33048
    Date        : 17/07/2016
    Time        : 13:26:45
    Type        : AddAutoPurchHdr
    Description : Could not save PurchHdr record - The supplier order number has already been used in Delivery Note No.1117103907 (Order No.1117103907), Supplier SupplierName(51)
    
    Line        : 33049
    Date        : 17/07/2016
    Time        : 13:26:45
    Type        : ImportASN
    Description : ConvertASNFiles: Failed to import GRN1171_0000700384_1117103907.xml.  Could not save PurchHdr record - The supplier order number has already been used in Delivery Note 
                  No.1117103907 (Order No.1117103907), Supplier SupplierName(51)
    

    正则表达式:

    Line\s+(\d+):\s+([^;]+);([^;]+);([^;]+);(.+)
    

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多