Powershell:在两个列之间的列中查找和附加值具有表和两个csv文件

时间:2015-12-14 05:40:23

标签: powershell powershell-v3.0

我将尝试简单地解决这个问题......

问题描述

2个csv文件(目标文件自动填写其他csv文件的字段)

我有一个包含3行的源csv文件:

sourceColumn1,sourceColumn2,sourceColumn3,sourceColumn4,sourceColumn5,sourceColumn6,sourceColumn7    ,sourceColumn8    ,sourceColumn9,sourceColumn10,sourceColumn11,sourceColumn12,sourceColumn13,sourceColumn14,sourceColumn15,sourceColumn16
sourceValue1 ,value2a      ,value3a      ,value4a      ,value5a      ,             ,value7a          ,sourceValueFound1,value9a      ,              ,              ,value12a      ,              ,              ,              ,
value1b      ,value2b      ,value3b      ,value4b      ,value5b      ,sourceValue2 ,                 ,sourceValueFound2,value9b      ,              ,              ,              ,value13b      ,              ,              ,
value1c      ,             ,value3c      ,value4c      ,value5c      ,sourceValue3 ,                 ,sourceValueFound3,value9c      ,              ,              ,              ,              ,              ,sourceValue3  ,

我有一个目标csv文件(在运行代码之前),包含3行:

"destinationColumn1","destinationColumn2","destinationColumn3","destinationColumn4","destinationColumn5","destinationColumn6"
"value1aa"          ,"value2aa"          ,"value3aa"          ,"sourceValue1"      ,"value5aa"          ,"AdValueFound1"
"value1bb"          ,"sourceValue2"      ,"value3bb"          ,"value4bb"          ,"sourceValue2"      ,"AdValueFound2"
"value1cc"          ,"sourceValue3"      ,"value3cc"          ,"value4cc"          ,"value5cc"          ,"No SourceValueFound"

结束目标

我希望在目标文件中添加一列destinationColumn7,其中sourceColumn8中的where(values -ne $ null)来自源文件。

通过从源文件中查找sourceValue#并将其与目标文件中的sourceValue#进行匹配来匹配正确的值。

所以目标文件应如下所示:

"destinationColumn1","destinationColumn2","destinationColumn3","destinationColumn4","destinationColumn5","destinationColumn6" ,"destinationColumn7"
"value1aa"          ,"value2aa"          ,"value3aa"          ,"sourceValue1"      ,"value5aa"          ,"AdValueFound1"      ,"sourceValueFound1"
"value1bb"          ,"sourceValue2"      ,"value3bb"          ,"value4bb"          ,"sourceValue2"      ,"AdValueFound2"      ,"sourceValueFound2"
"value1cc"          ,"sourceValue3"      ,"value3cc"          ,"value4cc"          ,"value5cc"          ,"No SourceValueFound","sourceValueFound3"

到目前为止我尝试了什么

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

Write-Host "Creating hash table with columns `"sourceColumn1`" and `"sourceColumn8`" From $sourceCsvFile"
$FirstHashTable = @{}
Import-Csv $sourceCsvFile | ForEach-Object {
    $FirstHashTable[$_.sourceColumn1] = If ($_.sourceColumn8) {$_.sourceColumn8} Else {'No SourceValueFound'}}
Write-Host "Complete."

Write-Host "Appending new destinationColumn7 column from hash table"
(Import-Csv $destinationCsvFile) |
    Select-Object -Property *, @{n='destinationColumn7';e={
    If ($FirstHashTable.ContainsKey($_.destinationColumn4)){
        $FirstHashTable[$_.destinationColumn4]
    } ElseIf ($FirstHashTable.ContainsKey($_.destinationColumn5)){
        $FirstHashTable[$_.destinationColumn5]
    } ElseIf ($FirstHashTable.ContainsKey($_.destinationColumn1)){
        $FirstHashTable[$_.destinationColumn1]
    } Else {
        'No MatchFound'
    }}} | Export-Csv "$destinationCsvFile-Temp" -NoType
Move-Item "$destinationCsvFile-Temp" $destinationCsvFile -Force
Write-Host "Complete."

目标文件的结果:

"destinationColumn1","destinationColumn2","destinationColumn3","destinationColumn4","destinationColumn5","destinationColumn6" ,"destinationColumn7"
"value1aa"          ,"value2aa"          ,"value3aa"          ,"sourceValue1"      ,"value5aa"          ,"AdValueFound1"      ,"sourceValueFound1"
"value1bb"          ,"sourceValue2"      ,"value3bb"          ,"value4bb"          ,"sourceValue2"      ,"AdValueFound2"      ,"No SourceValueFound"
"value1cc"          ,"sourceValue3"      ,"value3cc"          ,"value4cc"          ,"value5cc"          ,"No SourceValueFound","No SourceValueFound"

正如我们所看到的,这并没有在sourceValueFound#下附加destinationColumn7的最后两行。相反,他们是No SourceValueFound

接下来我注意到sourceValue#位于源文件的不同列中,sourceValueFound#不是$null

所以我对代码进行了更改......

$sourceCsvFile = 'C:\Temp\test1.csv'
$destinationCsvFile = 'C:\Temp\test2.csv'

Write-Host "Creating hash table with columns `"sourceColumn1`" and `"sourceColumn8`" From $sourceCsvFile"
$FirstHashTable = @{}
Import-Csv $sourceCsvFile | ForEach-Object {
$FirstHashTable[$_.sourceColumn1] = If ($_.sourceColumn8) {$_.sourceColumn8} Else {'No SourceValueFound'}}
Write-Host "Complete."

Write-Host "Creating hash table with columns `"sourceColumn6`" and `"sourceColumn8`" From $sourceCsvFile"
$SecondHashTable = @{}
Import-Csv $sourceCsvFile | ForEach-Object {
$SecondHashTable[$_.sourceColumn6] = If ($_.sourceColumn8) {$_.sourceColumn8} Else {'No SourceValueFound'}
}
Write-Host "Complete."


Write-Host "Appending new destinationColumn7 column from hash table"
(Import-Csv $destinationCsvFile) |
    Select-Object -Property *, @{n='destinationColumn7';e={
    If (($FirstHashTable.ContainsKey($_.destinationColumn4)) -and ($FirstHashTable.ContainsKey($_.destinationColumn4) -ne 'No SourceValueFound')) {
        $FirstHashTable[$_.destinationColumn4]
    } ElseIf (($FirstHashTable.ContainsKey($_.destinationColumn5)) -and ($FirstHashTable.ContainsKey($_.destinationColumn5) -ne 'No SourceValueFound')){
        $FirstHashTable[$_.destinationColumn5]
    } ElseIf (($FirstHashTable.ContainsKey($_.destinationColumn2)) -and ($FirstHashTable.ContainsKey($_.destinationColumn2) -ne 'No SourceValueFound')){
        $FirstHashTable[$_.destinationColumn2]
    } ElseIf ($SecondHashTable.ContainsKey($_.destinationColumn4)){
        $SecondHashTable[$_.destinationColumn4]
    } ElseIf ($SecondHashTable.ContainsKey($_.destinationColumn5)){
        $SecondHashTable[$_.destinationColumn5]
    } ElseIf ($SecondHashTable.ContainsKey($_.destinationColumn2)){
        $SecondHashTable[$_.destinationColumn2]
    } Else {
        'No MatchFound'
    }}} | Export-Csv "$destinationCsvFile-Temp.csv" -NoType
Write-Host "Complete."

目标文件的结果:

"destinationColumn1","destinationColumn2","destinationColumn3","destinationColumn4","destinationColumn5","destinationColumn6" ,"destinationColumn7"
"value1aa"          ,"value2aa"          ,"value3aa"          ,"sourceValue1"      ,"value5aa"          ,"AdValueFound1"      ,"No SourceValueFound"
"value1bb"          ,"sourceValue2"      ,"value3bb"          ,"value4bb"          ,"sourceValue2"      ,"AdValueFound2"      ,"sourceValueFound2"
"value1cc"          ,"sourceValue3"      ,"value3cc"          ,"value4cc"          ,"value5cc"          ,"No SourceValueFound","No SourceValueFound"

现在它在目标文件的第二行添加了sourceValueFound#,但No SourceValueFound的第三行仍为destinationColumn7

任何可以帮助我填写我所遗漏的内容的人都会非常感激。

1 个答案:

答案 0 :(得分:0)

我找到了答案,我没有跳过包含'No SourceValueFound'值的键,因为我使用的是($FirstHashTable.ContainsKey($_.destinationColumn4) -ne 'No SourceValueFound')而不是($FirstHashTable[$_.destinationColumn4] -ne 'No SourceValueFound')

然后我必须将其添加到其他3个ElseIf语句中,以确保在确保'No SourceValueFound'正确之前先检查所有非'No SourceValueFound'值。

$sourceCsvFile = 'C:\Temp\test1.csv'
$destinationCsvFile = 'C:\Temp\test2.csv'

Write-Host "Creating hash table with columns `"sourceColumn1`" and `"sourceColumn8`" From $sourceCsvFile"
$FirstHashTable = @{}
Import-Csv $sourceCsvFile | ForEach-Object {
    $FirstHashTable[$_.sourceColumn1] = If ($_.sourceColumn8) {$_.sourceColumn8} Else {'No SourceValueFound'}}
Write-Host "Complete."

Write-Host "Creating hash table with columns `"sourceColumn6`" and `"sourceColumn8`" From $sourceCsvFile"
$SecondHashTable = @{}
Import-Csv $sourceCsvFile | ForEach-Object {
    $SecondHashTable[$_.sourceColumn6] = If ($_.sourceColumn8) {$_.sourceColumn8} Else {'No SourceValueFound'}}
Write-Host "Complete."


Write-Host "Appending new destinationColumn7 column from hash table"
(Import-Csv $destinationCsvFile) |
    Select-Object -Property *, @{n='destinationColumn7';e={
If (($FirstHashTable.ContainsKey($_.destinationColumn4)) -and ($FirstHashTable[$_.destinationColumn4] -ne 'No SourceValueFound')) {
        $FirstHashTable[$_.destinationColumn4]
    } ElseIf (($FirstHashTable.ContainsKey($_.destinationColumn5)) -and ($FirstHashTable[$_.destinationColumn5] -ne 'No SourceValueFound')){
        $FirstHashTable[$_.destinationColumn5]
    } ElseIf (($FirstHashTable.ContainsKey($_.destinationColumn2)) -and ($FirstHashTable[$_.destinationColumn2] -ne 'No SourceValueFound')){
        $FirstHashTable[$_.destinationColumn2]
    } ElseIf (($SecondHashTable.ContainsKey($_.destinationColumn4)) -and ($SecondHashTable[$_.destinationColumn4] -ne 'No SourceValueFound')){
        $SecondHashTable[$_.destinationColumn4]
    } ElseIf (($SecondHashTable.ContainsKey($_.destinationColumn5)) -and ($SecondHashTable[$_.destinationColumn5] -ne 'No SourceValueFound')){
        $SecondHashTable[$_.destinationColumn5]
    } ElseIf (($SecondHashTable.ContainsKey($_.destinationColumn2)) -and ($SecondHashTable[$_.destinationColumn2] -ne 'No SourceValueFound')){
        $SecondHashTable[$_.destinationColumn2]
    } ElseIf ($FirstHashTable.ContainsKey($_.destinationColumn4)) {
        $FirstHashTable[$_.destinationColumn4]
    } ElseIf ($FirstHashTable.ContainsKey($_.destinationColumn5)){
        $FirstHashTable[$_.destinationColumn5]
    } ElseIf ($FirstHashTable.ContainsKey($_.destinationColumn2)){
        $FirstHashTable[$_.destinationColumn2]
    } Else {
        'No MatchFound'
    }}} | Export-Csv "$destinationCsvFile-Temp.csv" -NoType
Write-Host "Complete."

立即行动......