-1

This shows the contents of a .log file that logs the use of licenses from a specific vendor. From the log you can for example see that username_1 currently is using license 1, 2, & 3.

14:22:49 (VENDOR) OUT: "License_1" username_1@server459
14:22:49 (VENDOR) OUT: "License_2" username_1@server459
14:22:49 (VENDOR) OUT: "License_3" username_1@server459
14:22:49 (VENDOR) OUT: "License_1" username_2@server459
14:22:49 (VENDOR) OUT: "License_2" username_2@server459
14:22:49 (VENDOR) OUT: "License_3" username_2@server459
14:22:49 (VENDOR) OUT: "License_1" username_3@server459
14:46:32 (VENDOR) OUT: "License_2" username_3@server459
14:46:32 (VENDOR) OUT: "License_3" username_3@server459
14:47:40 (VENDOR) OUT: "License_3" username_4@server459

I want to create an output that shows how many of each license is in use and exclude all unnecessary data. I envision something like this:

License_1: 3
License_2: 3
License_3: 4
Henrik S.
  • 3
  • 3
  • 2
    So we don't suggest things you know don't work, what have you tried so far? Unless you specify that, your question is [off-topic](http://stackoverflow.com/help/on-topic) here. – Wai Ha Lee Jan 07 '16 at 09:52

3 Answers3

2

Assuming the license is the only element in double quotes you could use a regex like so:

Select-String YourFile.log -Pattern '(?<=")[^"]+(?=")' | % {
  $_.Matches.value
} | Group-Object | % {"$($_.name): $($_.count)"}
Dave Sexton
  • 10,768
  • 3
  • 42
  • 56
  • Thanks for your answer, Dave. The license is the only element in double quotes, so that worked like a charm. – Henrik S. Jan 07 '16 at 11:22
  • 1
    Cool, if my answer worked for you then don't forget to mark it as the excepted answer. – Dave Sexton Jan 07 '16 at 11:59
  • You should ask another questions not edit your original question otherwise the answers given will not make sense to others when viewing this question.. – Dave Sexton Jan 07 '16 at 14:04
2

A variation of Dave Sexton's answer using the -replace operator to extract the license substring from the logfile content:

(Get-Content 'C:\path\to\your.log') -replace '.*"(license_\d+)".*','$1' |
  Group-Object |
  Select-Object Name, Count

That will give you the license count in tabular format. You could pipe it into Format-Table to make it more condensed:

... | Format-Table -AutoSize

If you prefer the custom format from your question you could use the format operator (-f) like this:

... | ForEach-Object { '{0}: {1}' -f $_.Name, $_.Count }
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
1

If your log is always looks the same and separated with [Space] then you can use this simple manipulation:

$log = Get-Content C:\yourlog.txt
$Array = @()
Foreach ($line in $log)
{
    $Result = "" | Select Time,Vendor,"IN/OUT",License,Username
    $Split = $line -split '\s' | ? {$_}
    $Result.Time = ($Split)[0]
    $Result.Vendor = ($Split)[1]
    $Result."IN/OUT" = ($Split)[2]
    $Result.License = ($Split)[3]
    $Result.Username = ($Split)[4]
    $Array += $Result
}

Then you can show all the licenses by specific user:

$Array | ? {$_.Username -eq 'username_3@server459'} | Select -Expand License
Avshalom
  • 8,657
  • 1
  • 25
  • 43