使用 CSV 文件的比较附加 CSV
Append CSV using Comparisons of CSV Files
我正在尝试通过比较两个 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"
除了原来的内容,它什么也没有输出。
你的 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
我正在尝试通过比较两个 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"
除了原来的内容,它什么也没有输出。
你的 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