powershell - 如何使用 Powershell 过滤特定行和列的 csv 数据并计算总和?
问题描述
我是 Powershell 的新手,并尝试创建一个脚本,该脚本可以过滤 csv 文件中特定的信息行和列,并显示特定信息列的总和。
该工作表目前大约有 70 列和 3000 行
示例格式如下
Tracking # Project Activity Description 18-Mar 18-Apr 18-May 18-Jun 18-Jul
Tra_id_000 ABC Development 2 line summary of the work 100 50 10
Tra_id_001 DEF Development 2 line summary of the work 100 200 50
Tra_id_002 HIJ Testing 2 line summary of the work 50 10
Tra_id_003 KLM Requirement 2 line summary of the work 100
Tra_id_004 ABC Testing 2 line summary of the work 100
Tra_id_005 ABC Other 2 line summary of the work 1000
Tra_id_006 ABC Testing 2 line summary of the work 1000
我尝试使用https://www.pluralsight.com/blog/it-ops/powershell-excel-quick-tip上的示例来提供示例代码。
$data=import-csv -path '.\Test.csv'
$data|select-object -first 5|Format-Table -AutoSize
$data|select-object -property Project,'Activity'|group-object -property Prject,'Activity'|select-object -property name,count|sort-object -property name|format-table -autosize
在这里,我可以为项目和活动选择列并显示它们。
现在我被困在如何过滤特定项目和活动方面的下一步,然后计算相应月份的总和。例如,我只想过滤项目“ABC”和活动“测试”并显示计数以及显示 3 月 18 日、4 月 18 日等的总和
关于如何实现这一点的任何提示?
解决方案
对于过滤,您应该使用Where-Object
cmdlet。在过滤项目和活动列的示例中,您将使用:
$data | Where-Object { $_.Project -eq 'ABC' } | Format-Table
$data | Where-Object { $_.Project -eq 'ABC' -and $_.Activity -eq 'Testing' } | Format-Table
哪个会返回:
Tracking # Project Activity Description Mar Apr May Jun Jul
---------- ------- --------- ------------ --- --- --- --- ---
Tra_id_000 ABC Development 2 line summary of the work 100 50 10
Tra_id_004 ABC Testing 2 line summary of the work 100
Tra_id_005 ABC Other 2 line summary of the work 1000
Tra_id_006 ABC Testing 2 line summary of the work 1000
和:
Tracking # Project Activity Description Mar Apr May Jun Jul
---------- ------- --------- ------------ --- --- --- --- ---
Tra_id_004 ABC Testing 2 line summary of the work 100
Tra_id_006 ABC Testing 2 line summary of the work 1000
我不确定您是否想将每个日期列加在一起,但这里有一个使用您的数据的个人总和示例:
($data | Measure-Object Jun, May -sum).sum
这将返回每行的总和,每行 1:
1350
200
推荐阅读
- php - 从两个表中获取信息
- python - Python 中的 List-Then-Eliminate 实现
- r - 基于乘数复制行
- kubernetes - Kubernetes 'nginx 入口' 无法找到服务
- java - 尝试通过 HashSet 的构造函数传递 Comparator
- php - PHP 版本已弃用
- knockout.js - 使用油门时不会触发淘汰赛可观察到
- python - (virtual env)(global env) 为什么在我激活它时会发生这种情况,而不仅仅是 (virtual env)?
- twitter-bootstrap - 为什么卡内的 div 不会在 Bootstrap 中达到全高?
- node.js - 无法建立 websocket 连接 - React js + Node Express + Nginx