2016-08-11 1 views
1

私は、ログファイルを持っている(* .logの)私は以下のように解析し、クエリしたい:解析カスタムログファイル

Line 33043: 17/07/2016;13:26:45;GetMasterOrderNo;Master Order No is : 1117103907 for SoSupplierOrderNo, 1117103907 
Line 33048: 17/07/2016;13:26:45;AddAutoPurchHdr;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: 17/07/2016;13:26:45;ImportASN;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)

私が何をしたいのかは、次のようにヘッダをそれぞれの行を分割することです。

  • ライン、
  • 日、
  • 時間、
  • タイプ、
  • 説明

...これでクエリを実行できます。

これを行う最善の方法は何ですか?

+0

あなたはセミコロンで区切られたフィールドを持っています。セミコロン区切り文字でCSVとして解析できます。 –

+0

@ChrisDentそれは私の最初の試みでしたが、e。 g。ラインはセミコロンで区切られていないし、セミコロンも含めることができます... –

答えて

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

Regular expression visualization

6

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 
} 
+0

真実、私はバージョンを逃した! +1 –

+1

しかし、 '-Property'ハッシュテーブル引数はうまくいくはずですか? – Joey

+0

V2はデスクトップ上にあるものですが、V4を実行しているサーバー – JDGEEK

0
カスタムオブジェクトのハッシュテーブルのキーを作成する名前を持つ

使用正規表現のキャプチャグループ:

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 
} 
関連する問題