简体   繁体   English

比较两个csv文件中的列

[英]compare columns in two csv files

With all of the examples out there you would think I could have found my solution. 有了所有示例,您会认为我可以找到我的解决方案。 :-) :-)

Anyway, I have two csv files; 无论如何,我有两个csv文件; one with two columns, one with 4. I need to compare one column from each one using powershell. 一列包含两列,一列包含4。我需要使用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. 重要的是,我将第一个文件的row1与第二个文件的row1进行了比较。 I can't just do a simple -match or -contains. 我不能只是做一个简单的-match或-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): 我刚刚添加了以下检查,但得到的结果相同(所有情况均为False):

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

Using latkin's answer from here I think this would give you the result set you're looking for. 这里开始使用latkin的答案,我认为这将为您提供所需的结果集。 As per latkin's comment, the property comparison is redundant for your purposes but I left it in as it's good to know. 根据latkin的评论,属性比较对于您的目的是多余的,但我很乐意将其保留。 Additionally the header is specified even for the csv with headers to prevent the header row being included in the comparison. 另外,甚至为带有标题的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
}

In the second line, I see there a space "login id" and the first line doesn't have it. 在第二行中,我看到一个空格“ login id”,而第一行没有它。 Could that be an issue. 可能是一个问题。 Try having the same name for the headers in the .csv files itself. 尝试为.csv文件本身中的标题使用相同的名称。 And it works for without providing header or select statements. 它的工作原理是不提供标题或选择语句。 Below is my experiment based upon your input. 以下是根据您的输入进行的实验。

emp.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 empids.csv

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

PS C:\\>$newemp = Import-csv C:\\scripts\\emp.csv PS C:\\> $ newemp =导入CVS C:\\ scripts \\ emp.csv
PS C:\\>$ps = Import-CSV C:\\scripts\\empids.csv PS C:\\> $ ps =导入CSV C:\\ scripts \\ empids.csv
PS C:\\>$results = Compare-Object -ReferenceObject $newemp -DifferenceObject $ps | PS C:\\> $ results =比较对象-ReferenceObject $ newemp -DifferenceObject $ ps | foreach { $_.InputObject} foreach {$ _。InputObject}

Shows the difference objects that are not in $ps 显示不在$ ps中的差异对象

loginid  firstname  lastname   SideIndicator  
-------  ---------  --------   -------------  
k2340    Naam       mason      <=  
lk10j5   Shaan      kelso      <=  
303sk    Doug       smith      <=  

I am not sure if this is what you are looking for but i have used the PowerShell to do some CSV formatting for myself. 我不确定这是否是您要寻找的东西,但我已使用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}
                    }
                }

this is how my excel csv file looks like: 这是我的excel csv文件的样子:

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

and script outputs this: 脚本输出如下:

8
0

so basically script takes each number under Name column and then checks it against prod column. 因此,基本上,脚本会使用“名称”列下的每个数字,然后根据“产品”列进行检查。 If the number is there then it won't display else it will display that number. 如果该号码在那里,则不会显示其他号码,它将显示该号码。

I have also done it the opposite way: 我也以相反的方式做到了:

        $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}
                    }
                }
            }

this is how my excel csv looks like: 这是我的excel csv的样子:

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

and script outputs this: 脚本输出如下:

3
5
2

so script shows the matching entries only. 因此脚本仅显示匹配的条目。

You can play around with the code to look at different columns. 您可以使用代码来查看不同的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM