我在计算(PowerShell)txt文件中以下数据的平均值时遇到问题。
First row = EPOCH Time Second row = Response time (milliseconds) Third row = Name 1451603439,297,NA 1451603440,203,NA 1451604606,328,OP 1451604645,203,NA 1451604646,234,NC 1451604647,234,NA 1451604647,202,NA 1451604649,234,NA 1451604650,187,NA 1451604651,195,OP 1451604652,245,NA 1451604653,203,NA 1451604653,218,NA 1451604654,234,OP 1451604655,203,NA 1451604656,187,NA 1451604657,156,NA 1451604658,171,NA 1451604658,187,NA 1451604659,156,NA 1451604660,218,NA
我想计算每个名字每天的平均响应时间。
我面临的问题是我首先必须将时期计算到正常日期。 然后获取一天中的所有值并获取每个名称的平均响应时间,然后将其保存到另一个文件并重复此步骤直到文件结束。
答案 0 :(得分:1)
尝试这样的事情:
$origin = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
import-csv "C:\temp\file.TXT" -Header ColDate, ColNum, ColCode |
select ColNum, ColCode, @{N="ColDate"; E={$origin.AddSeconds($_.ColDate).Date }} |
group ColDate, ColCode |
select Name, @{N="ColAverage";E={($_.Group.ColNum | Measure-Object -Average).Average}}
答案 1 :(得分:0)
请找到可能的解决方案。此外,代码中还有一些注释,因此我们知道代码的每个部分正在做什么
#We get records from the file response.txt containing the format stuff,reponse-time,name
$file = Get-Content response.txt
#We create an array
$arr = [System.Collections.ArrayList]@()
#We will put each value separated by , into an object and will add each object in the array
Foreach ($result in $file) {
$tmp = $result.Split(",")
$obj = $null
$obj = New-Object System.Object
$obj | Add-Member -type NoteProperty -Name stuff -Value $tmp[0]
$obj | Add-Member -type NoteProperty -Name response -Value $tmp[1]
$obj | Add-Member -type NoteProperty -Name name -Value $tmp[2]
$arr += $obj
}
#We sort the array by name
$arr = $arr | Sort-Object name
$start=0
$i = 0
#we calculate average for each name until we reach the end of the array
while ($arr[$start]){
$counter=0
$tmpName=$arr[$start].name
$time=0
for($i=$start; $arr[$i].name -eq $tmpName; $i++,$counter++){
$time+=$arr[$i].response
}
Write-Host "Average time for all "$counter" records with name "$tmpName" is "($time/$counter)
$start=$i
}