我是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日等的总和
关于如何实现此目标的任何提示?
答案 0 :(得分:1)
对于筛选,应使用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