通过powershell比较两个文本文件或csv文件

时间:2014-01-23 16:13:45

标签: powershell csv compare

嗨Powershell专家,

我被我的剧本困住了。它没有给我任何错误,但我没有获得所需的输出。

我有两个文件文件或csv文件。这取决于我需要哪个文件我有两个选项我的数据只有字符串值和单列,所以,它可以是text或csv,无论哪个是必需的。我必须将每行的文件与另一个文件进行比较。无论数据位于哪一行。它必须每次都比较每一行。我尝试了很多次,但我没有得到理想的输出。我试图将它作为文本或csv进行比较,但所有时间都没有按要求给出输出。

Here is my code :(

$csv2 = "C:\Users\Desktop\Log\Threat_report.01232014195755.txt" 

$csv1 = "C:\Users\Desktop\Log\Threat_report.txt"

Compare-Object -ReferenceObject (get-content -path $csv1) -DifferenceObject (get-content -path $csv2)| export-csv "C:\Users\Desktop\Log\Threat_Report.csv" -NoTypeInfo

示例数据文件1

ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp aResource Menu.xlsm

ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp Rsfsesource Menu.xlsm

示例数据文件2

ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp Nomenu Menu.xlsm

ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp aResouserce Menu.xlsm

ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp Rsfsesource Menu.xlsm

ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp Rsfsesouaferce Menu.xlsm

当我比较文件时。脚本工作正常,但是当我对数据进行洗牌时,它会导出csv文件中的整个数据,如未找到匹配项。看起来它只是比较第1行到第1行,第2行到第2行等等。它没有检查第1行到第1,2,3,4行...... 请告诉我如何解决这个问题。

3 个答案:

答案 0 :(得分:0)

这是未经测试的,但它应该让你接近你想要的。

$Origin = Import-Csv "C:\Users\Desktop\Log\Threat_report.01232014195755.txt"
$Compare = Import-Csv "C:\Users\Desktop\Log\Threat_report.txt"

$Origin | % {
    $Source = $_
    [bool]$SourceFound = $false
    $Compare | % {
        #This will compare each row in Origin against each row in $Compare.
        #What do you want to do with the results?
        if(!$SourceFound)
        {
            if((Compare-Object -ReferenceObject $Source -DifferenceObject $_) -eq "==")
            {
                $SourceFound = $true
            }
        }
    }
    if(!$SourceFound)
    {
        #Here you Write-Output because $Source was not found
    }
}

答案 1 :(得分:0)

看看你在winmerge的输入......是否存在错别字?没有一行匹配。其中一条线说是资源,而“匹配”则表示aResouserce。

使用您的新输入,我创建了两个文本文件df1.txt和df2.txt。

 # Possible problem: what if the lines are not unique in the files?
 #  If this is the case, you'd have to not do the TODOs 
 #    or unique the lists before comparing them

 $csv1 = get-content -path ".\df1.txt"
 $csv2 = get-content -path ".\df2.txt"

 $list1      = new-object 'system.collections.generic.list[string]'
 $list2      = new-object 'system.collections.generic.list[string]'
 $listshared = new-object 'system.collections.generic.list[string]'

 $found = $false

 foreach ($line1 in $csv1)
 {
$found = $false
foreach ($line2 in $csv2)
{
    if ($line1 -eq $line2)
    {
        $listshared.add($line1)
        $found = $true
        # TODO: subtract $line1 from $csv1
        # TODO: subtract $line2 from $csv2
        break
    }
}

if (-not $found) 
{
    # TODO: subtract $line1 from $csv1
    $list1.add($line1)
}
 }

 # $csv1 and csv2 should be converted into real lists 
 # so they can be shrunk every time a match is found to reduce comparisons here
 foreach ($line2 in $csv2)
 {
$found = $false
foreach ($line1 in $csv1)
{
    if ($line1 -eq $line2)
    {
        # $listshared should be built at this point, just looking for uniques
        $found = $true
        break
    }
}
if (-not $found)
{
    $list2.add($line2)
}
 }

Write-Host "Things only in file 1:"
$list1

Write-Host "Things only in file 2:"
$list2

Write-Host "Things shared between the two files:"
$listshared

为我输出: H $。\ comp.ps1

仅在文件1中的内容: ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp aResource Menu.xlsm

仅在文件2中的内容:

ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp Nomenu Menu.xlsm ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp aResouserce Menu.xlsm ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp Rsfsesouaferce Menu.xlsm

两个文件之间共享的东西:

ONTAP_ADMIN $ \ vol \ ABCDEF \ Groupdata $ \ Central_Resource_Publish \ Temp data Menu \〜$ temp Rsfsesource Menu.xlsm

答案 2 :(得分:0)

这接近你想要的吗?

@'
ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp aResource Menu.xlsm 
ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp Rsfsesource Menu.xlsm
'@ | set-content file1.txt

@'
ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp Nomenu Menu.xlsm 
ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp aResouserce Menu.xlsm 
ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp Rsfsesource Menu.xlsm
ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp Rsfsesouaferce Menu.xlsm
'@ | set-content file2.txt

$base = get-content file1.txt

get-content file2.txt |
where {$base -notcontains $_} |
set-content diff.txt

get-content diff.txt

ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp Nomenu Menu.xlsm 
ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp aResouserce Menu.xlsm 
ONTAP_ADMIN$\vol\ABCDEF\Groupdata$\Central_Resource_Publish\Temp data Menu\~$temp Rsfsesouaferce Menu.xlsm