所有人的美好时光。
我的日志文件包含这样的行:
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
我使用以下Poweshell代码从此日志文件中提取信息:
$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
我只是拆分字符串并创建一个有趣属性的对象。
但我希望在灌浆后收集有关字节的信息。我可以添加另一个属性,并通过此cmdlet从日志文件中获取其值:
$objLine | Add-Member -type NoteProperty -name Bytes -value $substrings[9]
但是如何将此脚本产生的字节属性与分组对象相加以获得从精确域下载的字节总大小?
答案 0 :(得分:1)
我认为您正在寻找Measure-Object命令(别名为measure
)。它可用于计算管道中所有对象的给定属性的总和。
$bytesSum = ($events_list | measure -Sum Bytes).Sum
请注意,Measure-Object
实际上会返回Microsoft.PowerShell.Commands.GenericMeasureInfo
个对象。
关注OP编辑和评论:
您可以使用ForEach-Object
命令(别名为foreach
)迭代每个组,然后使用measure
命令计算给定组中所有项目的总和: / p>
$events_list | Group-Object -Property Domain | foreach { ($_.Group | measure -Sum Bytes).Sum }