Author Posts

September 23, 2016 at 9:32 am

Hi all,

i have to extract the total number of times a mail id occurred with the count . I want the top 5 list. example

Domain count
——- ——-
a@b.com 10
a@bb.com 3
a.4@c.com 2

here is the log file :

September 23, 2016 at 11:51 am

Very important in the future please do not post the content of internal log files without changing details like email addresses to something anonymous. You've just exposed hundreds of email addresses of your company to spam bots crawling the web.

A combination of RegEx and the Group-Object cmdlet usually works great for me if I need to extract and count the value of properties. I've created a sample script for you which works on the log file you've provided.

September 23, 2016 at 11:54 am

Have you tried anything? If you simply want occurrences of the email address, you should look at a REGEX pattern for email addresses. If you want the date as well, you can find regex pattern for the date format and use grouping and match both with a single REGEX pattern.

September 23, 2016 at 3:16 pm

# list top 5 results
$file = Get-ChildItem '\\path\to\logfile.log'
$users = switch -regex -file $file {
'User (.*) logged on' {$Matches[1]} 
}

$users | Group-Object | Sort-Object -Descending -Property count|
Select-Object Name,Count -First 5
}

September 23, 2016 at 3:37 pm

@random Cool. I was not aware of the -file parameter for the switch statement. Thanks very much for the example.