2017-04-06 78 views
-1

我的源CSV文件:閱讀CSV文件,按日期的數據,計算幾列該日期

 
"Name","timestamp","CPU|Demand (%)","CPU|Demand (%) (Trend)","CPU|Demand (%) (30 days forecast)" 
"BDC00-Management","Mar 2, 2017 12:01:22 AM","","30.68","" 
"BDC00-Management","Mar 2, 2017 12:10:00 AM","34.19","","" 
"BDC00-Management","Mar 2, 2017 12:16:22 AM","","30.68","" 
"BDC00-Management","Mar 2, 2017 12:20:00 AM","29.59","","" 
"BDC00-Management","Mar 3, 2017 6:55:00 AM","28.76","","" 
"BDC00-Management","Mar 3, 2017 7:00:00 AM","33.44","","" 
"BDC00-Management","Mar 3, 2017 7:01:22 AM","","30.98","" 
"BDC00-Management","Apr 1, 2017 7:01:22 PM","","","37.98" 
"BDC00-Management","Apr 1, 2017 7:21:22 PM","","","37.99" 
"BDC01-Horizon","Apr 2, 2017 2:56:22 AM","","","16.8" 
"BDC01-Horizon","Apr 2, 2017 3:06:22 AM","","","16.78" 
"BDC01-Linux","Mar 30, 2017 9:31:22 AM","","18.49","" 
"BDC01-Linux","Mar 30, 2017 9:40:00 AM","18.32","","" 
"BDC01-Linux","Mar 30, 2017 9:41:22 AM","","18.49","" 
"BDC01-Linux","Mar 31, 2017 1:30:00 PM","18.48","","" 
"BDC01-Linux","Mar 31, 2017 1:36:22 PM","","18.58","" 
"BDC01-Linux","Apr 1, 2017 9:51:22 PM","","","18.67" 
"BDC01-Linux","Apr 1, 2017 10:11:22 PM","","","18.68" 
"BDC01-Linux","Apr 2, 2017 4:16:22 AM","","","18.69" 
"BDC01-Linux","Apr 2, 2017 4:46:22 AM","","","18.7" 

我需要Export-Csv與每天的高數一個線路輸出每個「名稱」。例如:

 
"Name","timestamp","CPU|Demand (%)","CPU|Demand (%) (Trend)","CPU|Demand (%) (30 days forecast)" 
"BDC00-Management","Mar 2, 2017","34.19","30.68","" 
"BDC00-Management","Mar 3, 2017","33.44","30.98","" 
"BDC00-Management","Apr 1, 2017","","","37.99" 
"BDC01-Horizon","Apr 2, 2017","","","16.8" 
"BDC01-Linux","Mar 30, 2017","18.32","18.49","" 
"BDC01-Linux","Mar 31, 2017","18.48","18.58","" 
"BDC01-Linux","Apr 1, 2017","","","18.68" 
"BDC01-Linux","Apr 2, 2017","","","18.7" 

源文件具有超過75萬行,我需要減少在SharePoint自動化圖形報告的大小。這是很大的,我不需要每5分鐘的信息。

回答

2

最簡單的方法是使用Group-Object按名稱和日期對條目進行分組,並在您計算最大值的位置生成一個新對象。請注意,這需要將整個CSV讀入內存。它會很慢,併爲很多行使用大量內存。

評論在代碼中。嘗試:

#Read csv-input 
Import-Csv -Path "c:\old.csv" | 
#Group entries by server and date 
Group-Object Name, { ($_.timestamp -as [datetime]).Date } | 
ForEach-Object { 
    #Create new object per server per day with max-values 
    New-Object -TypeName psobject -Property ([ordered]@{ 
     Name = $_.Group[0].Name 
     timestamp = ($_.Group[0].timestamp -as [datetime]).ToString("MMM d, yyyy") 
     "CPU|Demand (%)" = $_.Group | Measure-Object -Property "CPU|Demand (%)" -Maximum | ForEach-Object { if($_.Maximum -gt 0) { $_.Maximum } } 
     "CPU|Demand (%) (Trend)" = $_.Group | Measure-Object -Property "CPU|Demand (%) (Trend)" -Maximum | ForEach-Object { if($_.Maximum -gt 0) { $_.Maximum } } 
     "CPU|Demand (%) (30 days forecast)" = $_.Group | Measure-Object -Property "CPU|Demand (%) (30 days forecast)" -Maximum | ForEach-Object { if($_.Maximum -gt 0) { $_.Maximum } } 
    }) 
} | Export-Csv -Path "c:\new.csv" -NoTypeInformation 

輸出:

"Name","timestamp","CPU|Demand (%)","CPU|Demand (%) (Trend)","CPU|Demand (%) (30 days forecast)" 
"BDC00-Management","mar 2, 2017","34,19","30,68","" 
"BDC00-Management","mar 3, 2017","33,44","30,98","" 
"BDC00-Management","apr 1, 2017","","","37,99" 
"BDC01-Horizon","apr 2, 2017","","","16,8" 
"BDC01-Linux","mar 30, 2017","18,32","18,49","" 
"BDC01-Linux","mar 31, 2017","18,48","18,58","" 
"BDC01-Linux","apr 1, 2017","","","18,68" 
"BDC01-Linux","apr 2, 2017","","","18,7" 
+0

哇..完美的作品 謝謝SOOOOO多:-) 你能推薦我一個電子書了這樣的東西對PowerShell的? 有美麗的一天:-) –

+0

不,對不起。最好的方法是學習PowerShell是如何工作的,然後你就可以自己搞清楚。請查看https://mva.microsoft.com/en-US/training-courses/getting-started-with-microsoft-powershell-8276和https://www.youtube.com/user/powershelldon/featured。另外,如果它解決了您的問題以解決問題,請記住接受答案。 :-) –