我有2个csv文件,我想从文件A到文件B添加一个新列到一个新文件。 ATM,它不会从A取值在powershell中合并2个csv文件
文件A.csv
ID Name
1 Peter
2 Dalas
文件B.CSV
Class
Math
Physic
新文件将是:
ID Name Class
1 Peter Math
2 Dalas Physics
这两个文件具有相同的行数。
如下面的我正在使用的代码,我想现在如何从文件中的取值,并把它放在文件B.
$CSV1 = Import-Csv ".\A.csv"
$CSV2 = Import-Csv ".\B.csv"
$CSV1 | ForEach-Object {
$Value= $_
$CSV2 | Select-Object *, @{Name='Information';Expression={ $Value}}
} | Export-Csv "C.csv" -NoTypeInformation
虽然标题建议重复内容的问题不同,因为它涉及基于它们的索引来加入**对象**(而不是文件)。考虑'$ CSV1 |加入$ CSV2 {$ LeftIndex -eq $ RightIndex)',未公开的功能:https://stackoverflow.com/questions/1848821/in-powershell-whats-the-best-way-to-join-two-tables-into -one/45483110#45483110 – iRon