gpt4 book ai didi

powershell - 按多列进行计数和分组

转载 作者:行者123 更新时间:2023-12-03 01:10:51 24 4
gpt4 key购买 nike

我有一个包含以下列的 CSV 文件:

  • 错误_ID
  • 日期
  • hh(两位数的小时)
  • 错误描述

它看起来像这样:

screenshot

在 SQL 中这非常简单:

SELECT X,Y,Count(1)
FROM #Table
GROUP BY X,Y

在 PowerShell 中,情况稍有不同。

最佳答案

Group-Object cmdlet 允许按多个属性进行分组:

Import-Csv 'C:\path\to\your.csv' | Group-Object ErrorID, Date

这会给你这样的结果:

Count Name          Group----- ----          -----    3 1, 15/07/2016 {@{ErrorID=1; Date=15/07/2016; Hour=16}, @{ErrorID=1; Da...    1 2, 16/07/2016 {@{ErrorID=2; Date=16/07/2016; Hour=9}}

However, to display grouped values in tabular form like an SQL query would do you need to extract them from the groups with calculated properties:

Import-Csv 'C:\path\to\your.csv' | Group-Object ErrorID, Date |
Select-Object @{n='ErrorID';e={$_.Group[0].ErrorID}},
@{n='Date';e={$_.Group[0].Date}}, Count

这将产生如下输出:

ErrorID Date       Count------- ----       -----1       15/07/2016     32       16/07/2016     1

关于powershell - 按多列进行计数和分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com