csv file group by col1 and display max value of col2
以下是我要从
获取数据的csv文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | hour, count ----------- 00,23 00,34 01,34 01,45 02,30 02,50 |
我想编写一个Power-shell脚本,在其中我得到以下输出
1 2 3 4 5 6 7 8 | hour,max(count) for unique hours -------------------------------- 00,34 01,45 02,50 |
我尝试使用组对象和度量对象进行分组,但是我无法获得上述期望的结果
1 | Import-Csv d:\\test\\serviceyrs.csv | Measure-Object -Property count -Minimum -Maximum |
为我提供最大和最小总数,但不是我想要的
您将获得的最大值如下:
1 2 3 | Import-Csv -Delimiter ',' -Path C:\\sample\\csv01.csv | Group-Object -Property hour | Select-Object -Property Name,@{N='Sum';E={($_.Group | Measure-Object -Property count -Maximum).Maximum }} |
1 2 3 4 5 6 7 | #file which needs to be inputted $revisedcsv="path\\of\\the\\filename\\plus_name"_revised.csv" #filename which would have the results $finalfilename="path\\of\\the\\filename\\plus_name+"_final.csv" Import-Csv $revisedcsv|group hour |select @{n='MaxCount';e={($_.group|measure count -Maximum).maximum}}| export-csv $finalfilename -notype |