I would like to use PowerShell to parse a log, and output to a CSV file.
What is the basic way to accomplish this?
Each log format involves it own logic when doing pattern matching, but I would propose to you something like:
(test.log is something like: date - context - type - msg)
$file = get-content test.log
$pattern = '^(.*) - (.*) - (.*) - (.*)$'
$findings = $file | select-string -pattern $pattern
$lines = foreach($f in $findings) {
if ($f -match $pattern) {
$line = @{
'date' = $matches[1]
'context' = $matches[2]
'level' = $matches[3]
'message' = $matches[4]
}
new-object -typename psobject -property $line
}
}
$lines | export-csv -notypeinformation -delimiter ';' -path 'test.csv'
do somethingin 4c74356b41's answer depends on the format of your log file. Eventually you can useimport-csv your.logoptionally with -header ColA ...