合并两个CSV-将CSV添加为另一列 [英] Combine two CSV's - Add CSV as another Column

查看:176
本文介绍了合并两个CSV-将CSV添加为另一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个csv合并在一起.其中一个CSV包含我要添加到另一列的列,但是在导出csv时,添加的列为空白.

I am trying to join together two csv's. One of the CSVs contain the column I want to add to the other but when the csv is exported the column added is blank.

$ csv1

VLAN
1
2
3

$ csv2

Host
NETMAN
ADMIN
CLIENT

我知道

VLAN, Host
1,
2,
3,

所需的输出是

VLAN, Host
1,NETMAN
2,ADMIN
3,CLIENT

我尝试过

$csv1 = Import-Csv -Path ".\VLAN.csv"
$csv2 = Import-Csv -Path ".\Hostname.csv"

$csv1 | % {$i=0} { $csv1[$i] | Add-Member -NotePropertyName Hostname -NotePropertyValue $csv2[$i++].Hostname;}

$csv1 | Export-Csv combined.csv -NoType

推荐答案

您要去的地方:

$csv1 = Import-Csv -Path ".\VLAN.csv"
$csv2 = Import-Csv -Path ".\Hostname.csv"

$arr = @()
#If($csv1.count -eq $csv2.count){
    for($i=0; $i -lt $csv1.Count; $i++){
        $temp = New-Object psobject
        $temp | Add-Member -MemberType NoteProperty -Name VLAN -Value $csv1[$i].Vlan
        $temp | Add-Member -MemberType NoteProperty -Name HOST -Value $csv2[$i].Host
        $arr+=$temp
    }
#}

$arr | Export-Csv combined.csv -NoTypeInformation

这篇关于合并两个CSV-将CSV添加为另一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆