【问题标题】:csv file group by col1 and display max value of col2csv 文件按 col1 分组并显示 col2 的最大值
【发布时间】:2018-04-23 04:22:12
【问题描述】:

以下是我需要从中获取数据的 csv 文件

hour, count
-----------

00,23

00,34

01,34

01,45

02,30

02,50

我想写一个 power-shell 脚本,我得到以下输出

hour,max(count) for unique hours
--------------------------------

00,34

01,45

02,50

我尝试使用 group-object 和 measure-object 进行分组,但我无法获得上述预期结果

Import-Csv d:\test\serviceyrs.csv | Measure-Object -Property count -Minimum -Maximum

给我最大和最小总数,但不是我想要的

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


    【解决方案1】:

    你会得到这样的最大值:

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

    【讨论】:

      【解决方案2】:
      #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
      

      【讨论】:

        猜你喜欢
        • 2016-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-12
        • 1970-01-01
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        相关资源
        最近更新 更多