源文件:
"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"
根据规则(在下面的代码中注释),文件将被过滤,并为每个ResultName添加一个新行。
代码
$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
#create new row
$newRow = New-Object PsObject -Property @{'SampleNumber' = $ND.SampleNumber; 'TypeIndicator'= $ND.TypeIndicator;
'ResultName' = $ND.ResultName + ' estimated' ;
'Indicator'=$ND.Indicator ; 'ResultEntry'='' }
#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
$newRow = New-Object PsObject -Property @{'SampleNumber' = $Ten.SampleNumber; 'TypeIndicator'= $Ten.TypeIndicator;
'ResultName' = $Ten.ResultName + ' estimated' ;
'Indicator'= $Ten.Indicator ; 'ResultEntry'='' }
}
#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)) {
$ND
$newRow = New-Object PsObject -Property @{'SampleNumber' = $ND.SampleNumber; 'TypeIndicator'= $ND.TypeIndicator;
'ResultName' = $ND.ResultName;
'Indicator'=$ND.Indicator ; 'ResultEntry'='' }
}
#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)) {
$Ten
$newRow = New-Object PsObject -Property @{'SampleNumber' = $Ten.SampleNumber; 'TypeIndicator'= $Ten.TypeIndicator;
'ResultName' = $Ten.ResultName;
'Indicator'=$Ten.Indicator ; 'ResultEntry'='' }
}
Write-Host $newRow #PROBLEM HERE: I dont know how to get this $newRow appended to my $fileData
这里:我不知道如何将$ newRow附加到我的$ fileData
问题 如何将$ newRow变量附加到$ fileData以便将所有值导出到文件?
答案 0 :(得分:1)
而不是Write-Host $newRow
只需输出新对象$newRow
即可将其附加到$ filedata。
顺便说一句,你的代码几乎无法读取,我会加强它。
使用您的上述示例数据,此代码为:
$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
#create new row
$newRow = New-Object PsObject -Property @{'SampleNumber' = $ND.SampleNumber;
'TypeIndicator'= $ND.TypeIndicator;
'ResultName' = $ND.ResultName + ' estimated' ;
'Indicator' = $ND.Indicator ;
'ResultEntry' = ''
}
#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
$newRow = New-Object PsObject -Property @{'SampleNumber' = $Ten.SampleNumber;
'TypeIndicator'= $Ten.TypeIndicator;
'ResultName' = $Ten.ResultName + ' estimated' ;
'Indicator' = $Ten.Indicator ;
'ResultEntry' = ''
}
}
#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)) {
$ND
$newRow = New-Object PsObject -Property @{'SampleNumber' = $ND.SampleNumber;
'TypeIndicator'= $ND.TypeIndicator;
'ResultName' = $ND.ResultName;
'Indicator' = $ND.Indicator ;
'ResultEntry' = ''
}
}
#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)) {
$Ten
$newRow = New-Object PsObject -Property @{'SampleNumber' = $Ten.SampleNumber;
'TypeIndicator'= $Ten.TypeIndicator;
'ResultName' = $Ten.ResultName;
'Indicator' = $Ten.Indicator ;
'ResultEntry' = ''
}
}
$newRow
# Write-Host $newRow #PROBLEM HERE: I dont know how to get this $newRow appended to my $fileData
}
$fileData | Format-Table
会给你这个输出:
SampleNumber TypeIndicator ResultName Indicator ResultEntry
------------ ------------- ---------- --------- -----------
111 10 S 125.234
111 10 S estimated
111 ND Sn 0.019
111 ND Sn estimated
111 ND Mo 0.278
111 ND Mo estimated
111 ND Cr 0.003
111 ND Cr
111 ND Sb 0.008
111 ND Sb
111 ND P 1.445
111 ND P estimated
111 ND Zn 0.045
111 ND Zn estimated