从logfile

时间:2016-04-28 07:54:16

标签: powershell full-text-search grouping logfile-analysis

我有一个包含许多条目的日志文件。其中一些以约会开始,另一些则不是。 我想搜索此/上个月的所有条目 行中的"UpgradeResource] part: 3-V12345678-12-"并计算按框分组的结果。 实际上有9个盒子从1到9计数但是如果我们买另一个盒子就会有10个或11个......盒子计数器总是在行尾的后面跟着-1。

我搜索的行看起来像这样:

2016-04-27 11:49:43,895 INFO  [ajp-apr-8009-exec-9] [com.xxx.shared.yyy.UpgradeResource] part: 3-V12345678-12-5-245, box: 3-V12345678-38-3-1
...
2016-04-27 11:49:43,895 INFO  [ajp-apr-8009-exec-9][com.xxx.shared.yyy.UpgradeResource] part: 3-V12345678-12-4-112, box: 3-V12345678-38-1-1

我的结果输出应为:

Month 03/2016:
Box 1: 10 times
Box 2: 123 times
Box 3: 65 times

Month 04/2016:
Box 1: 75 times
Box 2: 13 times
Box 3: 147 times

我不是很坚定地使用PowerShell并尝试了这个但是却出错并认为我的方法不正确:

$inputfile = "C:\temp\result.txt"
$matchstring = "(?\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*UpgradeResource] part: 3-V12345678-12-(?.*?), box: 3-V12345678-38-(\d{1})-1"
Get-Content $inputfile | foreach { 
    if ($_ -match $matchstring) {
        "" | select @{n='Date';e={$matches.date}},@{n='Key';e={$matches.Key}},@{n='PD';e={$matches.PD}}
    }
}

我得到的错误:

"(?\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*UpgradeResource] part:
3-V12345678-12-(?.*?), box: 3-V1001686-38-(\d{1})-1" wird analysiert -
Unbekanntes Gruppierungskonstrukt.
In C:\temp\count.ps1:16 Zeichen:6
+ if ($_ -match $matchstring)
+     ~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo : OperationStopped: (:) [], ArgumentException
     + FullyQualifiedErrorId : System.ArgumentException

2 个答案:

答案 0 :(得分:0)

那适合吗?

$inputfile = "C:\temp\result.txt"
$matchstring = "(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*UpgradeResource] part: 3-V12345678-12-(.*), box: 3-V12345678-38-(\d{1})-1"

Get-Content $inputfile | foreach { 
if ($_ -match $matchstring) {

    "" | select @{n='Date';e={$matches.1}},@{n='Key';e={$matches.2}},@{n='PD';e={$matches.3}}
}

}

给我输出:

Date                                                    Key                                                     PD                                                    
----                                                    ---                                                     --                                                    
2016-04-27 11:49:43                                     5-245                                                   3                                                     
2016-04-27 11:49:43                                     4-112                                                   1   

答案 1 :(得分:0)

您获得的错误是因为(?...)不是有效的分组构造。如果要使用命名组(代码的其余部分建议),则问号必须后跟角括号中的组名称((?<name>...))。对于非捕获组,必须后跟冒号((?:...))。

有关详细信息,请参阅here

您的代码应该看起来像这样:

$inputfile   = 'C:\temp\result.txt'
$matchstring = '(?<date>\d{4}-\d{2}-\d{2}) (?<time>\d{2}:\d{2}:\d{2})' +
               '.*UpgradeResource] ' +
               'part: 3-V12345678-12-(?<Key>.*?), ' +
               'box: 3-V12345678-38-(?<PD>\d{1})-1'
Get-Content $inputfile | Where-Object {
  $_ -match $matchstring
} | ForEach-Object {
  New-Object -Type PSObject -Property  @{
    'Date' = $matches.date
    'Time' = $matches.time
    'Key'  = $matches.Key
    'Box'  = 'Box ' + $matches.PD
  }
} | Group-Object Date, Box