解析文本文件

时间:2016-04-06 16:55:16

标签: string parsing powershell

如何解析此text1.txt:

2016-04-06 12:02:32 AM - INFO  – Connected to services
2016-04-06 12:02:47 AM - ERROR – Service exception
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Pooled connection request timed out (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
Oracle.ManagedDataAccess.Client.OracleException: Pooled connection request timed out
at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword)
at Oracle.ManagedDataAccess.Client.OracleConnection.Open()
2016-04-06 12:02:47 AM - WARN  – Unexpected error has occurred. See application logs for more details. Service will wait for 60 seconds and then try again.
2016-04-06 12:07:07 AM - INFO  – Connected to services
2016-04-06 12:07:22 AM - ERROR – Service exception
System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Pooled connection request timed out (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
Oracle.ManagedDataAccess.Client.OracleException: Pooled connection request timed out
at OracleInternal.ConnectionPool.PoolManager`3.Get(ConnectionString csWithDiffOrNewPwd, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
at OracleInternal.ConnectionPool.OraclePoolManager.Get(ConnectionString csWithNewPassword, Boolean bGetForApp, String affinityInstanceName, Boolean bForceMatch)
at OracleInternal.ConnectionPool.OracleConnectionDispenser`3.Get(ConnectionString cs, PM conPM, ConnectionString pmCS, SecureString securedPassword, SecureString securedProxyPassword)
at Oracle.ManagedDataAccess.Client.OracleConnection.Open()
2016-04-06 12:07:22 AM - WARN  – Unexpected error has occurred. See application logs for more details. Service will wait for 60 seconds and then try again.

这是我目前的代码:

function parse-log
{
    param(
        [string]$line
    )

    $data = $line.split(' ') 
    $dateString = '{0} {1} {2}' -f $data[0], $data[1], $data[2]
    $timeStamp = Get-Date -Date $dateString
    [pscustomobject]@{
        TimeStamp = $timeStamp
        Client    = $data[3]
        Message   = $data[4]
    }
}

foreach ( $line2 in $lines2 )
{
    $entry = parse-log -line $line2
}

我收到错误,因为它试图解析下一行。我需要它来解析下一个日期时间。另外,如何分割连字符(-),以便得到以下内容?

$timestamp = 2016-04-06 12:02:32 AM
$data[3] = INFO or ERROR
$data[4] = the rest of the string

3 个答案:

答案 0 :(得分:2)

如果您只想处理以日期开头的行并跳过其他行,您可以执行以下操作:

$pattern = '^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [AP]M) - (\w+) +– (.*)'
$lines2 | Where-Object { $_ -match $pattern } | ForEach-Object {
    [PSCustomObject]@{
        TimeStamp = $matches[0]
        Severity  = $matches[1]
        Message   = $matches[2]
    }
}

请注意,第二个短划线不是连字符,而是连字符(字符U + 2013)。

答案 1 :(得分:0)

我建议您使用正则表达式来解析日志:

(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\w{2})\s-\s(\w+)\s+–\s(.+?)(?=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\w{2}\s-\s|$)

Demo

这里是剧本:

$content = Get-Content "PATH_TO_YOUR_LOG" -Encoding UTF8

$regex = '(?si)(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\w{2})\s-\s(\w+)\s+–\s(.+?)(?=\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\s\w{2}\s-\s|$)'

$entries = [regex]::Matches($content, $regex)

foreach ($entry in $entries)
{
    [PSCustomObject]@{
        TimeStamp =  $entry.Groups[1].Value
        Level  = $entry.Groups[2].Value
        Message   =  $entry.Groups[3].Value
    }
}

输出:

TimeStamp              Level Message                                                                                                                                                       
---------              ----- -------                                                                                                                                                       
2016-04-06 12:02:32 AM INFO  Connected to services                                                                                                                                         
2016-04-06 12:02:47 AM ERROR Service exception System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Pooled connection request timed out (Fault Detail is equal to ...
2016-04-06 12:02:47 AM WARN  Unexpected error has occurred. See application logs for more details. Service will wait for 60 seconds and then try again.                                    
2016-04-06 12:07:07 AM INFO  Connected to services                                                                                                                                         
2016-04-06 12:07:22 AM ERROR Service exception System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Pooled connection request timed out (Fault Detail is equal to ...
2016-04-06 12:07:22 AM WARN  Unexpected error has occurred. See application logs for more details. Service will wait for 60 seconds and then try again.  

答案 2 :(得分:0)

使用switch语句的一个更直接的例子,它在AWK之后被图案化(在某种程度上)。

INSERT INTO test (bla) VALUES (1),(2)