如果符合其中一个条件,我想修改列名ResultName
$file = gci "q:\test\2018\03\27\sample.*"
$fileData = Import-Csv $file.FullName | Group-Object ResultName | Foreach-Object
{
$Ten = $_.Group | Where-Object { $_.TypeIndicator -eq '10' }
$ND = $_.Group | Where-Object { $_.TypeIndicator -eq 'ND' }
# Case 1: If Indicator is null or ! for /10 and Indicator is null or ! for ND
# Select line ND and create new row with ResultName + estimated Eg."Al estimated" with ResultEntry = ''
if(('!','' -contains $Ten.Indicator) -and ('!','' -contains $ND.Indicator))
{
$ND
#Case 2: If Indicator is null/! for 10 and Indicator is not null for ND.
# Select line ND and create new row with ResultName + estimated Eg."Al estimated" with ResultEntry = ''
}
elseif (('!','' -contains $Ten.Indicator) -and ('o','u' -contains $ND.Indicator))
{
$Ten
}
#Case 3: If Indicator contains either 'o' or 'u' for 10 and Indicateur is null/! for ND
# select line ND and create a new row with ResultEntry = ''
elseif (('o','u' -contains $Ten.Indicator) -and ('!','' -contains $ND.Indicator))
{
$newValue = $ND | Select-Object -Property @{Name='SampleNumber';Expression={$ND.SampleNumber}},
@{Name='TypeIndicator';Expression={$ND.TypeIndicator}},
@{Name='ResultName';Expression={$ND.ResultName + ' estimed'}},
@{Name='ResultEntry';Expression={$ND.ResultEntry}},
@{Name='Indicator';Expression={$ND.Indicator}}
}
#Case 4: If both /10 and ND contains either o or u, Select line /10
# select line 10 and create a new row with ResultEntry = ''
elseif (('u','o' -contains $Ten.Indicator) -and ('o','u' -contains $ND.Indicator))
{
$newValue = $Ten | Select-Object -Property @{Name='SampleNumber';Expression={$Ten.SampleNumber}},
@{Name='TypeIndicator';Expression={$Ten.TypeIndicator}},
@{Name='ResultName';Expression={$Ten.ResultName + ' estimed'}},
@{Name='ResultEntry';Expression={$Ten.ResultEntry}},
@{Name='Indicator';Expression={$Ten.Indicator}}
}
$newValue
}
$fileData | Format-Table
我能够获得前3个案例的输出,但是对于最后一个案例,数据似乎重复了几次。我不明白为什么最后一个案例会重复。
源文件:
"SampleNumber","TypeIndicator","ResultName","Indicator","ResultEntry"
"111","10","S","","125.234"
"111","10","Sn","","0.034"
"111","10","Mo","","0.307"
"111","10","Cr ","u","0.022"
"111","10","Sb","u","-0.096"
"111","10","P","","1.593"
"111","10","Zn","","0.126"
"111","ND","S","o","113.920"
"111","ND","Sn","","0.019"
"111","ND","Mo","","0.278"
"111","ND","Cr ","","0.003"
"111","ND","Sb","","0.008"
"111","ND","P","","1.445"
"111","ND","Zn","","0.045"
答案 0 :(得分:0)
问题解决了,我没有分配给变量,我只需要将对象传递给Select-Object并更改列名
$ND | Select-Object -Property @{Name='SampleNumber';Expression={$ND.SampleNumber}}, etc...