使用CSV文件比较附加CSV

时间:2016-03-22 02:18:46

标签: csv powershell comparison-operators

我正在尝试使用两个csv的比较将数据输入到一个csv中。数据是两个csvs之间比较的结果。

例如:

CSV 1说

Host,Type
Host1,Server
Host2,Switch
Host3,Hub
Host4,Router

CSV 2说:

List of Types, Hosts
              ,Host1 random letters Host2
              ,random letters Host3 Host2
              ,Host4 Host3 Host2

输出应为

List of Types,Hosts
server switch,Host1 random letters Host2
hub switch,random letters Host3 Host2
router hub switch,Host4 Host3 Host2

这就是我所拥有的:

我的问题似乎是比较部分

  $CSV1 = Import-Csv "Pathname1.csv"

Foreach ($title in $CSV1)
{
    $Hosts =$title."Hosts"
    $Type=$title."Type"
}

$csv2 = Import-Csv "Pathname2.csv"

foreach($title in $CSV2)
{
    $Typelist = $title."List of Types"
    $Hostlist = $title."Hosts"

    if ($Hostlist -contains $Hosts)
    {
    $title.'List of Types'= $Type
    }
 }

 $csv2 | export-csv "export.csv"

除了最初的内容之外什么都不输出。

1 个答案:

答案 0 :(得分:1)

你的csv文件对我们的组织方式真的没有多大意义,但这并不意味着你的问题无法通过一些思考和学习如何解决你的问题来解决,并通过调试器来使其工作

我已经编写了一些代码来提供所需的输出,甚至添加了几行可能有用的调试语句(输出),可以取消注释以查看进度。

<强> CSV1

Host,Type
Host1,Server
Host2,Switch
Host3,Hub
Host4,Router

<强> CSV2

List of Types,Hosts
,Host1 random1 letters1 Host2
,random2 letters2 Host3 Host2
,Host4 Host3 Host2

<强>代码

"-=-=-=-=-=-=-= start"

$content1 = Get-Content d:\junk\c1.csv
$data1 = $content1 | ConvertFrom-Csv -Header @('Host','Type')
"c1===="
$content1
"csv1===="
$data1 | ft

$content2 = Get-Content d:\junk\c2.csv
$data2 = $content2 | ConvertFrom-Csv -Header @('Type','Hosts')
"c2===="
$content2
"csv2===="
$data2 | ft

#"data===="
$output = @()
foreach ($hostList in ($data2.Hosts | select -Skip 1)){
    # don't use variable name $host. it is a reserved variable.
    # see about_Automatic_Variables
    # https://technet.microsoft.com/en-us/library/hh847768.aspx
    $subHosts = $hostList -split ' '

    #"subhosts===="
    #$subHosts

    $types = @()
    foreach ($subHost in $subHosts)
    {
        #$("sh = $subHost")
        $typeMatch = (($data1 | select -Skip 1) | where {$_.Host -eq $subHost}).Type

        if ($typeMatch)
        {
            $types += $typeMatch
        }
    }
    $types = $types -join ' '

    #"types===="
    #$types

    $matches = @{}
    $matches.Add($types, $hostList)
    $output += $matches
}

"final list===="
$output

"-=-=-=-=-=-=-=- end"

<强>输出

-=-=-=-=-=-=-= start
c1====
Host,Type
Host1,Server
Host2,Switch
Host3,Hub
Host4,Router
csv1====

Host  Type  
----  ----  
Host  Type  
Host1 Server
Host2 Switch
Host3 Hub   
Host4 Router

c2====
List of Types,Hosts
,Host1 random1 letters1 Host2
,random2 letters2 Host3 Host2
,Host4 Host3 Host2
csv2====

Type          Hosts                       
----          -----                       
List of Types Hosts                       
              Host1 random1 letters1 Host2
              random2 letters2 Host3 Host2
              Host4 Host3 Host2           

final list====

Name              Value                       
----              -----                       
Server Switch     Host1 random1 letters1 Host2
Hub Switch        random2 letters2 Host3 Host2
Router Hub Switch Host4 Host3 Host2           
-=-=-=-=-=-=-=- end