[英]Making statistics CSV using Powershell
Suppose I create a script that creates a long csv table like this: 假设我创建了一个脚本,该脚本创建了一个长的csv表,如下所示:
Tag Value Date Time Mode Alarm
PPP_VE0160A.F_CV 5.04E-03 5/1/2014 12:00:00 AM 11 Alm Disabled
PPP_VE0160A.F_CV 4.95E-03 5/1/2014 12:01:00 AM 11 Alm Disabled
PPP_VE0160A.F_CV 5.16E-03 5/1/2014 12:02:00 AM 11 Alm Disabled
.
.
.
PPP_VE0160B.F_CV 1.42E-02 5/1/2014 12:00:00 AM 11 Alm Disabled
PPP_VE0160B.F_CV 1.46E-02 5/1/2014 12:01:00 AM 11 Alm Disabled
PPP_VE0160B.F_CV 1.26E-02 5/1/2014 12:02:00 AM 11 Alm Disabled
.
.
.
PPP_VE0161A.F_CV 4.43E-03 5/1/2014 12:00:00 AM 11 Alm Disabled
PPP_VE0161A.F_CV 3.16E-03 5/1/2014 12:01:00 AM 11 Alm Disabled
PPP_VE0161A.F_CV 4.13E-03 5/1/2014 12:02:00 AM 11 Alm Disabled
.
.
.
And I want to make a csv file with statistics info, like this: 我想用统计信息制作一个csv文件,如下所示:
Tag Minimum Average Maximum
PPP_VE0160A.F_CV 4.95E-03 5.12E-03 5.25E-03
PPP_VE0160B.F_CV 1.23E-02 1.35E-02 1.51E-02
PPP_VE0161A.F_CV 4.07E-03 4.32E-03 4.51E-03
.
.
.
I looked at using Measure-Object cmdlet, but I don't know how to separate it for individual tags. 我看过使用Measure-Object cmdlet,但我不知道如何将其分隔为单个标签。 See below for my code so far: 到目前为止,我的代码见下文:
Param ( [Datetime]$date = ( Get-Date ).AddDays( -1 ) )
$currentPath = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
$sourcePath = ""
$destinationPath = "$currentPath\Output_Files"
# Only using a few from long list of tagnames, for testing
$tagnames = @(
"PPP_VE0160A", "PPP_VE0160B", "PPP_VE0161A"
)
$dateInput = $date.ToString( "MM/dd/yy" )
$dateOutput = $date.ToString( "yyMMdd" )
ForEach ( $tagname in $tagnames ) {
.\HTD2CSV\HTD2CSV.exe `
PPP:$tagname.F_CV `
/dur:00:23:59:00 `
/int:00:01:00 `
/sd:$dateInput `
/st:00:00:00 `
/sp:$sourcePath `
/dp:$destinationPath\$tagname.csv `
/dtf:1 `
/dbg:0
}
Wait-Process htd2csv
$data = Get-ChildItem $destinationPath\PPP_*.csv |
ForEach-Object { $_.FullName } |
Import-Csv -Header Tag, Value, Date, Time, Mode, Alarm
Remove-Item $destinationPath\PPP_*.csv
$data | Export-Csv -Path "$destinationPath\$dateOutput summary.csv"
I tried using something like 我尝试使用类似
$data | Group Tag | ForEach-Object {
Measure-Object $_.Group.Value -Property Value -Minimum -Average -Maximum
} | Export-Csv -Path "$destinationPath\$dateOutput statistics.csv"
And this only produced blank csv file. 而这仅产生空白的csv文件。 I am not sure how to group up all the statistics information before exporting it. 我不确定如何在导出之前对所有统计信息进行分组。
After thinking and playing around for a long time, I figured this out on my own. 经过长时间的思考和玩耍,我自己解决了这个问题。 The Measure-Object expects an object from the pipeline. Measure-Object需要管道中的对象。 So, I wrote this way to get the result I wanted: 所以,我写了这样的方式来获得想要的结果:
$collection = @()
$data | Group-Object Tag | ForEach-Object {
$datarow = New-Object PSObject -Property @{ Tag = $_.Name }
$stat = $_.Group | Measure-Object -Property Value -Minimum -Average -Maximum
$datarow | Add-Member NoteProperty -Name "Minimum" -Value $stat.Minimum
$datarow | Add-Member NoteProperty -Name "Average" -Value $stat.Average
$datarow | Add-Member NoteProperty -Name "Maximum" -Value $stat.Maximum
$collection += $datarow
}
$collection | Export-Csv -Path "$destinationPath\$dateOutput statistics.csv" -NoTypeInformation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.