通过将值与数组和条件匹配,在csv中追加现有列

时间:2015-12-23 20:05:46

标签: powershell powershell-v3.0

我会尽我所能尽力打破这一点。

到目前为止我的工作是什么:

目前我有两个csv文件......

test1.csv

test1ColumnN,test1ColumnI,test1ColumnD,selectDomainOne,selectDomainTwo,selectDomainThree
asdf,asdf,asdf,,,
nValue1,iValue1,dValue1,sValue1,,
qwer,asdf,zxcv,,,
nValue2,iValue2,dValue2,,,
qwer,zxcv,asdf,lkjh,,
nValue3,iValue3,dValue3,sValue3,,
zxcv,qwer,asdf,,poiu,
nValue1,iValue1,dValue1,,sValue1,
nValue4,iValue4,dValue4,,sValue4,
asdf,qwer,zxcv,fghj,mnbv,
nValue5,iValue5,dValue5,,,
asdf,cvbn,erty,,,uytr
nValue7,iValue7,dValue7,,,sValue7
nValue8,iValue8,dValue8,,,sValue8
nValue9,iValue9,dValue9,,,sValue9
qwer,asdf,zxcv,poiu,lkjh,mnbv

test2.csv

DomainCatagories,test2ColumnS,test2ColumnA,test2ColumnN,test2ColumnI,test2ColumnD
DomainOne,sValue1,aValue1,nValue1,,dValueN
DomainOne,sValue2,aValue2,,iValue2,dValue2
DomainOne,sValue3,aValue2,nValue3,iValue3,dValue3
DomainTwo,sValue1,aValue2,,iValue1,dValueN
DomainTwo,sValue4,aValue1,nValue4,,dValueN
DomainTwo,sValue5,aValue1,nValue5,iValue5,dValue5
DomainThree,sValue7,aValue2,nValue7,iValue7,dValue7
DomainThree,sValue8,aValue1,nValue8,iValue8,dValue8
DomainThree,sValue9,aValue2,nValue9,iValue9,dValue9

现在我想添加一个列(在test2.csv中)以匹配来自test1.csv和test2.csv的sValue#,其条件为(test2.csv中的$_.DomainCatagories='DomainOne')和(来自test1.csv的$_.selectDomainOne

为此,我使用以下代码......

#Create Column
$domainNameOne = @{}
$domainNameOne = Import-Csv 'C:\Scripts\Tests\test1.csv' | Where-Object {$_.selectDomainOne} | Select-Object -Expand 'selectDomainOne'

(Import-Csv 'C:\Scripts\Tests\test2.csv') | 
    Select-Object -Property *, @{n='Test1sValues';e={
    if($_.DomainCatagories -eq 'DomainOne'){
        if(($domainNameOne -contains $_.test2ColumnS) -and ($_.test2ColumnS)){
            $_.test2ColumnS
        } Else {
            'Not found in test1'
    }}}} | Export-Csv "C:\Scripts\Tests\test2-Temp" -NoType
    Move-Item "C:\Scripts\Tests\test2-Temp" 'C:\Scripts\Tests\test2.csv' -Force

运行代码后,我得到以下test2.csv(isCorrect)...

"DomainCatagories","test2ColumnS","test2ColumnA","test2ColumnN","test2ColumnI","test2ColumnD","Test1sValues"
"DomainOne","sValue1","aValue1","nValue1","","dValueN","sValue1"
"DomainOne","sValue2","aValue2","","iValue2","dValue2","Not found in test1"
"DomainOne","sValue3","aValue2","nValue3","iValue3","dValue3","sValue3"
"DomainTwo","sValue1","aValue2","","iValue1","dValueN",""
"DomainTwo","sValue4","aValue1","nValue4","","dValueN",""
"DomainTwo","sValue5","aValue1","nValue5","iValue5","dValue5",""
"DomainThree","sValue7","aValue2","nValue7","iValue7","dValue7",""
"DomainThree","sValue8","aValue1","nValue8","iValue8","dValue8",""
"DomainThree","sValue9","aValue2","nValue9","iValue9","dValue9",""

我所拥有的不起作用:

接下来,我运行以下代码......

#Append Column
$domainNameThree = @{}
$domainNameThree = Import-Csv 'C:\Scripts\Tests\test1.csv' | Where-Object {$_.selectDomainThree} | Select-Object -Expand 'selectDomainThree'

(Import-Csv 'C:\Scripts\Tests\test2.csv') | % {
    if($_.DomainCatagories -eq 'DomainThree'){
        if(($domainNameThree -contains $_.test2ColumnS) -and ($_.test2ColumnS)){
            $_.Test1sValues = $_.test2ColumnS
        } Else {
            $_.Test1sValues = 'Not found in test1'
    }}} | Export-Csv "C:\Scripts\Tests\test2-Temp" -NoType
Move-Item "C:\Scripts\Tests\test2-Temp" 'C:\Scripts\Tests\test2.csv' -Force

不是在正确的行中添加值,而是完全删除整个文件并将其保存为empty file

结束目标

我想要代码生成的是这个(通知值填写在最后一列的最后3行)...

"DomainCatagories","test2ColumnS","test2ColumnA","test2ColumnN","test2ColumnI","test2ColumnD","Test1sValues"
"DomainOne","sValue1","aValue1","nValue1","","dValueN","sValue1"
"DomainOne","sValue2","aValue2","","iValue2","dValue2","Not found in test1"
"DomainOne","sValue3","aValue2","nValue3","iValue3","dValue3","sValue3"
"DomainTwo","sValue1","aValue2","","iValue1","dValueN",""
"DomainTwo","sValue4","aValue1","nValue4","","dValueN",""
"DomainTwo","sValue5","aValue1","nValue5","iValue5","dValue5",""
"DomainThree","sValue7","aValue2","nValue7","iValue7","dValue7","sValue7"
"DomainThree","sValue8","aValue1","nValue8","iValue8","dValue8","sValue8"
"DomainThree","sValue9","aValue2","nValue9","iValue9","dValue9","sValue9"

我在第二段代码中做错了什么?

1 个答案:

答案 0 :(得分:0)

您从显示的示例我所拥有的不起作用:缺少关键部分。 Export-Csv会将所有内容传输到其中以填充CSV,但您不提供任何内容。

问题是你没有通过管道传递任何东西。仅仅更新一个属性。最简单的方法是在$_语句后添加if。或者您可以在另一个问题中使用之前已经完成的计算属性。 Compare dates with different formats in csv file下面的示例甚至使用if语句。

Import-Csv $csvFile | Select-Object *, @{n='MatchDates';e={ if((([datetime]$_.Date1).Date -eq $_.Date3) -and (([datetime]$_.Date2).Date -eq $_.Date3) -and (([datetime]$_.Date1).Date -eq $_.Date2)){ 'Match Found' }Else{ 'No Match Found' }}} | 
    Export-Csv "$csvFile-results.csv" -NoTypeInformation -Force