简体   繁体   English

选择字符串多个变量模式

[英]Select-string multiple variable patterns

I am trying to extract error lines from a log file which are defined by two things. 我试图从由两件事定义的日志文件中提取错误行。 The log file line looks like this: 日志文件行如下所示:

2018-05-22 06:25:35.309 +0200 (Production,S8320,DKMdczmpOXVJtYCSosPS6SfK8kGTSN1E,WwObvwqUw-0AAEnc-XsAAAPR) catalina-exec-12 : ERROR com.tableausoftware.api.webclient.remoting.RemoteCallHandler - Exception raised by call target: User 2027 does not have permissions to view comments for view 13086. (errorCode=1)
com.tableausoftware.domain.exceptions.PermissionDeniedException: User 2027 does not have permissions to view comments for view 13086. (errorCode=1)

The error is described in two lines, so I need to filter the error and the current hour and then copy it into a file. 错误在两行中描述,因此我需要过滤错误和当前时间,然后将其复制到文件中。 This code does does copy all the errors, but not only from the current hour. 这段代码确实复制了所有错误,但不仅限于当前时间。

$hodina = (Get-Date -UFormat "%H").ToString()
$hodina = " " + $hodina +":"
$err = ": ERROR"
$errors = Select-String -Path "D:\..\file.log" -Pattern $hodina, $err -Context 0, 1
echo ($errors).Line >> Errors_file.txt

So I was wondering, how to put multiple variables into -Pattern , or if there is another solution to this problem. 所以我想知道如何将多个变量放入-Pattern ,或者是否有解决此问题的另一种方法。

Here is how to get all of the matching lines: 这是获取所有匹配行的方法:

Get-Content "file.log" | 
    Select-String -Pattern "^(?:(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})).* : ERROR (?:(.*))$" |
        ForEach-Object {
            [PsCustomObject]@{
                TimeStamp=(Get-Date $_.Matches.Groups[1].Value)
                LineNumber=$_.LineNumber
                Error=$_.Matches.Groups[2].Value
            }
        }

This will give you output like this: 这将为您提供如下输出:

TimeStamp           LineNumber Error                                                                                                                                              
---------           ---------- -----                                                                                                                                              
22/05/2018 06:25:35          1 com.tableausoftware.api.webclient.remoting.RemoteCallHandler - Exception raised...
22/05/2018 06:25:35          4 com.tableausoftware.api.webclient.remoting.RemoteCallHandler - Exception raised...
22/05/2018 06:25:35          8 com.tableausoftware.api.webclient.remoting.RemoteCallHandler - Exception raised...
22/05/2018 06:25:35         10 com.tableausoftware.api.webclient.remoting.RemoteCallHandler - Exception raised...

If you only want the items where the hour of the timestamp matches the current hour, modify the code like this: 如果只希望时间戳的小时与当前小时相匹配的项目,请修改代码,如下所示:

Get-Content "file.log" | 
    Select-String -Pattern "^(?:(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})).* : ERROR (?:(.*))$" |
        ForEach-Object {
            [PsCustomObject]@{
                TimeStamp=(Get-Date $_.Matches.Groups[1].Value)
                LineNumber=$_.LineNumber
                Error=$_.Matches.Groups[2].Value
            }
        } | Where-Object {$_.TimeStamp.Hour -eq (Get-Date).Hour}

You can then send the output to file, or better (if you plan to manipulate them later in PowerShell), CSV ( Export-Csv ) or CliXml ( Export-CliXml ) 然后,您可以将输出发送到文件,或者更好(如果您打算稍后在PowerShell中操作它们),CSV( Export-Csv )或CliXml( Export-CliXml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM