0

With all of the examples out there you would think I could have found my solution. :-)

Anyway, I have two csv files; one with two columns, one with 4. I need to compare one column from each one using powershell. I thought I had it figured out but when I did a compare of my results, it comes back as false when I know it should be true. Here's what I have so far:

 $newemp = Import-Csv -Path "C:\Temp\newemp.csv" -Header login_id, lastname, firstname, other | Select-Object "login_id"
 $ps = Import-Csv -Path "C:\Temp\Emplid_LoginID.csv" | Select-Object "login id"
 If ($newemp -eq $ps)
    {
      write-host "IDs match" -forgroundcolor green
    }
 Else 
    {
      write-host "Not all IDs match" -backgroundcolor yellow -foregroundcolor black
    }

I had to specifiy headers for the first file because it doesn't have any. What's weird is that I can call each variable to see what it holds and they end up with the same info but for some reason still comes up as false. This occurs even if there is only one row (not counting the header row).

I started to parse them as arrays but wasn't quite sure that was the right thing. What's important is that I compare row1 of the first file with with row1 of the second file. I can't just do a simple -match or -contains.

EDIT: One annoying thing is that the variables seem to hold the header row as well. When I call each one, the header is shown. But if I call both variables, I only see one header but two rows.

I just added the following check but getting the same results (False for everything):

    $results = Compare-Object -ReferenceObject $newemp -DifferenceObject $ps -PassThru | ForEach-Object { $_.InputObject }
4

3 回答 3

3

使用 latkin's answer from here我认为这将为您提供您正在寻找的结果集。根据 latkin 的评论,属性比较对于您的目的来说是多余的,但我把它留在了,因为很高兴知道。此外,甚至为带有标题的 csv 也指定了标题,以防止标题行包含在比较中。

$newemp = Import-Csv -Path "C:\Temp\_sotemp\Book1.csv" -Header loginid | 
    Select-Object "loginid"

$ps = Import-Csv -Path "C:\Temp\_sotemp\Book2.csv" -Header loginid | 
    Select-Object "loginid"

#get list of (imported) CSV properties
$props1 = $newemp | gm -MemberType NoteProperty | select -expand Name | sort
$props2 = $ps | gm -MemberType NoteProperty | select -expand Name | sort

#first check that properties match 
#omit this step if you know for sure they will be
if(Compare-Object $props1 $props2){
    throw "Properties are not the same! [$props1] [$props2]"
}

#pass properties list to Compare-Object
else{
    Compare-Object $newemp $ps -Property $props1
}
于 2013-08-21T17:57:34.667 回答
2

在第二行中,我看到有一个空格“登录 ID”,而第一行没有。会不会是个问题。尝试为 .csv 文件本身中的标题使用相同的名称。它适用于不提供标题或选择语句的情况。以下是我根据您的输入进行的实验。

emp.csv

loginid      firstname  lastname
------------------------------
abc123   John       patel  
zxy321   Kohn       smith  
sdf120   Maun       scott  
tiy123   Dham       rye  
k2340    Naam       mason  
lk10j5   Shaan      kelso  
303sk    Doug       smith  

empids.csv

loginid
-------  
abc123  
zxy321  
sdf120  
tiy123  

PS C:\>$newemp = Import-csv C:\scripts\emp.csv
PS C:\>$ps = Import-CSV C:\scripts\empids.csv
PS C:\>$results = Compare-Object -参考对象 $newemp -DifferenceObject $ps | foreach { $_.InputObject}

显示不在 $ps 中的差异对象

loginid  firstname  lastname   SideIndicator  
-------  ---------  --------   -------------  
k2340    Naam       mason      <=  
lk10j5   Shaan      kelso      <=  
303sk    Doug       smith      <=  
于 2013-08-21T18:01:23.947 回答
2

我不确定这是否是您要查找的内容,但我已使用 PowerShell 为自己进行了一些 CSV 格式设置。

            $test = Import-Csv .\Desktop\Vmtools-compare.csv
                foreach ($i in $test) {
                    foreach ($n in $i.name) {    
                        foreach ($m in $test) {
                            $check = "yes"         
                            if ($n -eq $m.prod) {
                                $check = "no"
                                break
                            }
                        }
                    if ($check -ne "no") {$n}
                    }
                }

这就是我的 excel csv 文件的样子:

prod    name
1       3
2       5
3       8
4       2
5       0

和脚本输出这个:

8
0

所以基本上脚本在 Name 列下获取每个数字,然后根据 prod 列检查它。如果数字在那里,那么它将不会显示,否则它将显示该数字。

我也以相反的方式做到了:

        $test = Import-Csv c:\test.csv                
            foreach ($i in $test) {
                foreach ($n in $i.name) {                    
                    foreach ($m in $test) {
                        $check = "yes"                         
                        if ($n -eq $m.prod) {echo $n}
                    }
                }
            }

这就是我的 excel csv 的样子:

prod    name
1       3
2       5
3       8
4       2
5       0

和脚本输出这个:

3
5
2

所以脚本只显示匹配的条目。

您可以使用代码来查看不同的列。

于 2013-10-16T18:15:22.313 回答