Paul Smith
Paul Smith

Reputation: 529

How to extract several string from log files

I am trying to extract several strings from log files. So far so good. But I don't know how to extract only the required details from that result.

Example

A log entry looks like this:

(DIAG:XMLRPC)(11:07:01 15/04/2019)(MEM:130590)(TID:3632)(USER:Administrator)(REMOTE:10.67.125.177:59032)(XmlRpc: called method 'QueryCreativeFilterInfoList'.31)

I can parse it with the the following code:

$output_file = 'C:\Copy-Test\logins.txt'
$regex = 'QueryCreativeFilterInfoList'
$files = Get-ChildItem "C:\Copy-Test\Logs"

foreach ($file in $files)
 {
    gc $file.FullName | select-string -Pattern $regex | Select-String -Pattern "Administrator" | Out-File -FilePath $output_file -Append
}

Now I would like to only output the data, time username but I don't know how. I saw some crazy regex stuff but that was way above of my skill level.

I would appreciate if someone could guide me on this

Upvotes: 1

Views: 450

Answers (1)

montonero
montonero

Reputation: 1721

The easiest way would be to split the log entry string by )( delimiter and then print members of a resulting array. Something like this:

gc $file.FullName | select-string -Pattern $regex | Select-String -Pattern "Administrator"| foreach {
   $a=$_ -split "\)\("
   "$(($a[1] -split " ")[1,0] -join ' ') $($a[4].substring(5))"
}

Upvotes: 2

Related Questions