1

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

第 33043 行:17/07/2016;13:26:45;GetMasterOrderNo;Master Order No 是: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)中使用

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

  • 线,
  • 日期,
  • 时间,
  • 类型,
  • 描述

...所以我可以对此执行查询。

这样做的最佳方法是什么?

4

3 回答 3

6

对马丁非常好的答案进行了一些修复。[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
}
于 2016-08-11T09:05:36.860 回答
6

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

$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+([^;]+);([^;]+);([^;]+);(.+)

正则表达式可视化

于 2016-08-11T08:58:42.073 回答
0

使用带有名称的 Regex 捕获组来为自定义对象创建哈希表键:

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
}
于 2016-08-11T10:47:45.377 回答