csv文件中的powershell筛选器记录集
假设我有一个简单的csv文件。该模型包含4个字段csv文件中的powershell筛选器记录集,powershell,csv,Powershell,Csv,假设我有一个简单的csv文件。该模型包含4个字段id、name、group和sub。group字段的整数值介于1和15之间,可以重复 所以我要做的是选择第7组和第9组以外的所有记录……我编写了以下代码: $csv = Import-Csv -Path names.csv -Header @('id', 'name', 'group', 'sub') $total = $csv | select group -unique write-host "total:", $total.length $i
id
、name
、group
和sub
。group字段的整数值介于1和15之间,可以重复
所以我要做的是选择第7组和第9组以外的所有记录……我编写了以下代码:
$csv = Import-Csv -Path names.csv -Header @('id', 'name', 'group', 'sub')
$total = $csv | select group -unique
write-host "total:", $total.length
$ignore = $csv | where { $_.sub -eq 'a'}
write-host "total to ignore:", $ignore.length
$ignoreGroups = $ignore | select group -unique
write-host "Groups to ignore:", $ignoreGroups.length
$workingGroups = $csv | where {$ignore -notcontains $_ } | select group -unique
write-host "Working groups count:", $workingGroups.Length
前一行报告错误的结果
我的目标是一次处理属于$csv
一个组的记录(即$workingGroups
中的组)
Ps:这只是一个模型,不是真实的数据。真正的数据是一个巨大的日志文件,我必须处理
样本数据:
编辑:
我试图使用选择对象
来选择distict组
值,但我得到的是一个对象数组(每个对象都具有组
属性)。我该怎么做才能将不同的组值单独作为一个整数数组
“所以我要做的是选择第7组和第7组以外的所有记录
9“
我真的不明白你的代码试图做什么
在您的评论之后:
$groups = $csv | ?{ $_.sub -ne "a" } | group-object group
然后,您可以通过以下方式访问组:
$groups | %{ $_.Name; $_.Group | %{ $_ /*Do whatever you want with each group member here */} }
这是我的理解
要求
- 处理CSV数据
- 排除sub=“a”中的所有记录
- 排除第7组和第9组
- 按组分组剩余数据以进行处理
代码
$WorkingList = $csv | where { ($_.sub -ne "a") -and (@(9,7) -notcontains $_.group)}
$WorkingGroups = $WorkingList | group-object group
这就是采样数据最终的样子
PS>$WorkingGroups
计数名称组
----- ---- -----
2{{id=5;name=Carlene Preston;group=2;sub=b},{id=9;name=Mccullough-Fisher;group=2;sub=b}
23{{id=10;name=Tara Gallagher;group=3;sub=b},{id=12;name=Morton Whitley;group=3;sub=b}
14{{id=4;name=Shannon Donovan;group=4;sub=b}
15{{id=1;name=Chandler-Cox;group=5;sub=b}
18{{id=3;name=Brown Gardner;group=8;sub=b}
3 11{{id=0;name=Eleanor Whitney;group=11;sub=b},{id=2;name=Georgette Simpson;group=11;sub=b},{id=7;name=Holland Mccarty;group=11;sub=b}
1 12{{id=14;name=Butler-Hale;group=12;sub=b}
114{{id=8;name=Larson-Larson;group=14;sub=b}
这和你想要的差不多吗
编辑:
要简单地处理数据,请遍历组和项
foreach ($group in $WorkingGroups){
write-host "Processing Group: $($group.Name)"
foreach ($item in $group.Group){
write-host "`tProcessing Item: $($item.Name)"
}
}
结果
Processing Group: 2
Processing Item: Carlene Preston
Processing Item: Mccullough Fisher
Processing Group: 3
Processing Item: Tara Gallagher
Processing Item: Morton Whitley
Processing Group: 4
Processing Item: Shannon Donovan
Processing Group: 5
Processing Item: Chandler Cox
Processing Group: 8
Processing Item: Brown Gardner
Processing Group: 11
Processing Item: Eleanor Whitney
Processing Item: Georgette Simpson
Processing Item: Holland Mccarty
Processing Group: 12
Processing Item: Butler Hale
Processing Group: 14
Processing Item: Larson Larson
EDIT2
数据中已经内置了一个组数组。要访问组数组,请执行以下操作:
$WorkingGroups.Name
另一种方法:使用-ExpandProperty
获取纯数组,而不是对象[]
$workingGroups = $csv | where {$_.sub -ne 'a'} | select -ExpandProperty group -unique
foreach($group in $workingGroups) {
$recordset = $csv | where { $_.group -eq $group }
#do something with $recordset
}
可以说,2条记录的组=1,5条记录的组=2,等等…目标是分别处理每个记录集…但首先我必须从混合中排除第7组和第9组…根据其7和第9组的数据…在我的工作场所,我需要排除sub=AEExcluding sub=a YELDS group 7和9的所有记录顺便说一句…一旦我排除了这些记录(第7组和第9组或更多),它可能会产生更多的记录。我必须处理剩余的记录-“集”-每组属于一个组…即。我必须按组处理记录集我希望得到一个不同组值的数组…然后我需要逐个循环它们。Select object始终返回一个对象数组…@DeoWalk我添加了处理数据的简单代码。我仍然不明白这些要求是否正确。。。
$workingGroups = $csv | where {$_.sub -ne 'a'} | select -ExpandProperty group -unique
foreach($group in $workingGroups) {
$recordset = $csv | where { $_.group -eq $group }
#do something with $recordset
}