0

这很难解释,但我会尽力而为。

我有 2 个名为 file1.csv 和 file2.csv 的 CSV 文件...

我想要的是file1.csv尝试将其第一列中的条目与file2.csv. 如果存在匹配项,那么我希望 File1.csv 中的行覆盖 .csv 中的特定行file2.csv。我不介意它是否输出到另一个文件。请帮忙 :)

文件1:

uniqueID, first, surname, address
1, Joe, Bloggs, 32 Cheddar Rd 
2, Bill, Gates, 21 SOmewhere Road
3, Tim, Burton, 24 This Lane

文件2:

uniqueID, first, surname, address
1, Joe, Bloggs, 34 BLAH BLAH
2, Bill, Gates, 43 BLAH BLAH
3, Tim, Burton, 65 BLAH BLAH
4, Kai, Peters, 42 BLAH BLAH
5, Gay, Thomas, 65 BLAH BLAH

输出:

uniqueID, first, surname, address
1, Joe, Bloggs, 32 Cheddar Rd 
2, Bill, Gates, 21 SOmewhere Road
3, Tim, Burton, 24 This Lane
4, Kai, Peters, 42 BLAH BLAH
5, Gay, Thomas, 65 BLAH BLAH

谢谢斯宾塞

4

1 回答 1

1

你应该展示一些你试图获得帮助的代码,但由于这是你的第一篇文章,我会帮助你。

$file1 = Import-Csv file1.txt
$file2 = Import-Csv file2.txt

$file2 | % {$Entry = $_; $file1 | ? {$_.uniqueID -eq $Entry.uniqueID} | % {$Entry.address = $_.address}}

$Results = @()

$file2  | % {$Results += New-Object -TypeName PSObject -Property @{"uniqueID" = $_.uniqueID; "first" = $_.First; "surname" = $_.surname; "address" = $_.address} }

$Results | Export-Csv -Path file3.csv -NoTypeInformation
于 2013-09-26T09:54:19.907 回答