Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Parse log files using context

$
0
0

Hello,
I am new to Powershell and am trying to parse data from multiple log files.  I was wondering if you gurus could help me.  The data of interest is contained in two successive lines and interspersed throughout the files. 

 There may be no hits on some log files and hundreds of hits on other log files:
 Line-1 has date and time
 Line-2 has the data.

The log format is like this:
Line-1    2015-09-11 01:29:24.13 "System data"                        [EXEC]
Line-2    'CODES'           EXEC   1234ABCD 5678ABCD 9012ABCD 3456ABCD 1234ABCD -1234 A ABC-Dataset-A 1  I

I would like the output in a CSV format with individual columns and titles looking something like this:
Date             time     code-1   code-2   code-3    code-4   code-5     Code-6       dataset  code
2015-09-11 01:29:24 1234ABCD 5678ABCD 9012ABCD 3456ABCD 1234ABCD   ABC   Dataset   A

I've searched the internet and have found several scripts that I was able to modify and get me started but, I have a long way to go!  Any assistance and guidance would be greatly appreciated!

$logfile = Get-Content c:\Tool_Files-Logs\*.log
for ($i = 0; $i -lt $logfile.count; $i++) {
    if ($logfile[$i] -match "'CODES'") {
        if ($logfile[$i - 1] -match "\d{4}-\d{2}-\d{2}" ) {
            $logfile[($i - 1)..($i + 0)] | Out-File  'C:\scripts\Logfile_Parsed.CSV'
        }
    }
    }


Thanks in advance,
Tiago


Viewing all articles
Browse latest Browse all 6937

Trending Articles