Reputation: 55
How can I parse this 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.
This is my current code:
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
}
I get errors because it's trying to parse the next line. I need it to parse the next datetime. In addition, how can I split at hyphens (-
) so I get the following?
$timestamp = 2016-04-06 12:02:32 AM
$data[3] = INFO or ERROR
$data[4] = the rest of the string
Upvotes: 1
Views: 226
Reputation: 2639
A perhaps more direct example using the switch statement which was patterned (to some extent) after AWK.
# Switch statement loops over each line in the file
switch -regex (get-content data.txt)
{
# Makes sure that the line matches
'^[0-9]+-[0-9]+-[0-9]' {
# split the line with multiple assignement & type constraint
[datetime] $timestamp, $data1, $data2 = $_ -split ' . '
# Build the result object
[PSCustomObject] @{
timestamp = $timestamp
data1 = $data1
data2 = $data2
}
}
}
Upvotes: 0
Reputation: 200483
If you just want to process lines starting with a date and skip over the other lines, you could do something like this:
$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]
}
}
Note that the second dash is not a hyphen, but an en-dash (character U+2013).
Upvotes: 2
Reputation: 59001
I would recommend you to use a regex to parse the log:
(\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|$)
And here the script:
$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
}
}
Output:
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.
Upvotes: 0