2017-01-23 98 views
-1

我在計算(PowerShell)txt文件中以下數據的平均值時遇到了問題。從EPOCH時間戳計算PowerShell中的平均值

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

按行,您實際上是指列嗎?至於如何轉換曆元時間取決於它是秒,毫秒還是其他。第一個值映射到1970年1月17日(ms)或2015年12月31日(秒)。 – vonPryz

+3

歡迎來到StackOverflow!這裏的*實際*問題是什麼,你試過了什麼?正如這個「問題」所代表的,看起來你只是在等人爲你寫一個腳本。 StackOverflow是*不*一個免費的腳本編寫服務 –

+0

您可以開始[這裏](http://stackoverflow.com/questions/10781697/convert-unix-time-with-powershell)。 – sodawillow

回答

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}} 
+0

出於興趣,EPOCH時間參考是afaik主要是Utc的基礎。所以平均值不是基於當地日期,而是以Utc偏移量轉移的24小時週期。 +1的解決方案。 – LotPings

+0

完美,像魅力一樣工作。我試圖先轉換 - >替換每個EPOCH時間,然後我將它分組。但這更有意義 –

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 
}