简体   繁体   中英

PowerShell. Group-object usage

Good day for all.

I have log file with lines like this:

192.168.0.28 - - [10/Feb/2014:16:24:33 +0400] "GET http://yabs.yandex.ru/count/SkPKYklqq7a40000ZhP7Z2y5KfK1cm9kGxS193E8jW1RNmQ9frz84PXWdPSZ3QPeYg8taCczkTWB0m6g8wMlFhK2lA6enWAD0P6uxymm1e-pXw430f-Z_uc4auKDcGL2Z90-IQ2WMc82hv0-IPIJQIgdaouLgBOaDFy5fB00000DhlaDKclHOmpo1B41ieGGkPSZ3RlXnNxwbZbWAtaI HTTP/1.1" 302 868 TCP_MISS:DIRECT
192.168.0.69 - - [10/Feb/2014:16:24:34 +0400] "GET http://cdn.v.rtr-vesti.ru/_cdn_auth/secure/v/vh/vod_hls/definst/smil:vh/smil/967/362_d20140204202900.smil/media-b1296000_25.ts? HTTP/1.1" 200 1552555 TCP_MISS:DIRECT
192.168.0.31 - - [10/Feb/2014:16:24:34 +0400] "GET http://57.img.avito.st/140x105/446703657.jpg HTTP/1.1" 200 3674 TCP_MISS:DIRECT
192.168.0.31 - - [10/Feb/2014:16:24:34 +0400] "GET http://52.img.avito.st/140x105/628845352.jpg HTTP/1.1" 200 2836 TCP_MISS:DIRECT
192.168.0.29 - - [10/Feb/2014:16:24:35 +0400] "GET http://kad.arbitr.ru/Content/Static/Css/Common/cssie8.css HTTP/1.1" 404 2436 TCP_NEGATIVE_HIT:NONE
192.168.0.28 - - [10/Feb/2014:16:24:35 +0400] "GET http://www.google.com/jsapi HTTP/1.1" 200 6534 TCP_MISS:DIRECT

I use the following Poweshell code to extract information from this log file:

$events_list = @()

Foreach ($line in gc 'D:\Downloads\test.txt') {
$substrings = [regex]::split($line,' ')
$cropped_url = [regex]::split($substrings[6],'/')

if ($cropped_url.Count -gt 4) {continue} 

$domain = $cropped_url[0] + "//" + $cropped_url[2]
$date = $substrings[3] -replace "\[", ""
$objLine = New-Object System.Object
$objLine | Add-Member -type NoteProperty -name IP -value $substrings[0]
$objLine | Add-Member -type NoteProperty -name Date -value $date
$objLine | Add-Member -type NoteProperty -name Domain -value $domain
$events_list += $objLine

}


$events_list | Group-Object -Property Domain | Sort-Object -Property Count -Descending | ft Count, Name -AutoSize >> D:\Downloads\domains.txt

I just split that strings and create an object with interesting properties.

But i want gather info about bytes after groupping. I can add another property, and obtain its value from the log file by this cmdlet:

$objLine | Add-Member -type NoteProperty -name Bytes -value $substrings[9]

But how to sum this bytes property resulting from this script over grouped object to get the total size of bytes, downloaded from exact domains?

I think you're looking for the Measure-Object command (aliased as measure ). It can be used to calculate the sum of a given property for all objects in the pipeline.

$bytesSum = ($events_list | measure -Sum Bytes).Sum

Note that Measure-Object actually returns a Microsoft.PowerShell.Commands.GenericMeasureInfo object.

Following OP edits and comment:

You can use the ForEach-Object command (aliased as foreach ) to iterate over each group and then use the measure command to calcute the sum for all items within a given group:

$events_list | Group-Object -Property Domain | foreach { ($_.Group | measure -Sum Bytes).Sum }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM