比较PowerShell的两个文本文件或CSV文件



hi powershell专家,

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

我有两个文件文本文件或CSV文件。这取决于我所需的哪个文件,我既有选项我的数据都在字符串值中,并且仅在单列中,因此可以是文本或CSV,以所需的为准。我必须将文件与另一个文件进行比较。无论数据都位于哪个线上。它将每次都必须比较每一行。我尝试了很多次,但我没有得到所需的输出。我试图将其与文本或CSV进行比较,但一直都没有按要求给我输出。

Here is my code :(
$csv2 = "C:UsersDesktopLogThreat_report.01232014195755.txt" 
$csv1 = "C:UsersDesktopLogThreat_report.txt"
Compare-Object -ReferenceObject (get-content -path $csv1) -DifferenceObject (get-content -path $csv2)| export-csv "C:UsersDesktopLogThreat_Report.csv" -NoTypeInfo

示例数据文件1

ontap_admin $ vol abcdef groupdata $ central_resource_publish temp数据菜单 $ temp aresource菜单菜单.xlsm

ontap_admin $ vol abcdef groupdata $ central_resource_publish temp data菜单〜$ temp rsfsesource菜单菜单.xlsm

示例数据文件2

ontap_admin $ vol abcdef groupdata $ central_resource_publish temp数据菜单 $ temp nomenu菜单

ontap_admin $ vol abcdef groupdata $ central_resource_publish temp数据菜单 $ temp aresouserce菜单菜单.xlsm

ontap_admin $ vol abcdef groupdata $ central_resource_publish temp data菜单〜$ temp rsfsesource菜单菜单.xlsm

ontap_admin $ vol abcdef groupdata $ central_resource_publish temp数据菜单〜$ temp rsf rsfseouaferce菜单

当我比较文件时。脚本工作正常,但是当我将数据供电时,它会像找不到匹配一样导出整个数据。似乎仅将第1行与第1行,第2行与第2行进行比较。它不像第1行1、2、3、4 ...检查。请告知我如何解决此问题。

这是您想要的东西吗?

@'
ONTAP_ADMIN$volABCDEFGroupdata$Central_Resource_PublishTemp data Menu~$temp aResource Menu.xlsm 
ONTAP_ADMIN$volABCDEFGroupdata$Central_Resource_PublishTemp data Menu~$temp Rsfsesource Menu.xlsm
'@ | set-content file1.txt
@'
ONTAP_ADMIN$volABCDEFGroupdata$Central_Resource_PublishTemp data Menu~$temp Nomenu Menu.xlsm 
ONTAP_ADMIN$volABCDEFGroupdata$Central_Resource_PublishTemp data Menu~$temp aResouserce Menu.xlsm 
ONTAP_ADMIN$volABCDEFGroupdata$Central_Resource_PublishTemp data Menu~$temp Rsfsesource Menu.xlsm
ONTAP_ADMIN$volABCDEFGroupdata$Central_Resource_PublishTemp 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$volABCDEFGroupdata$Central_Resource_PublishTemp data Menu~$temp Nomenu Menu.xlsm 
ONTAP_ADMIN$volABCDEFGroupdata$Central_Resource_PublishTemp data Menu~$temp aResouserce Menu.xlsm 
ONTAP_ADMIN$volABCDEFGroupdata$Central_Resource_PublishTemp data Menu~$temp Rsfsesouaferce Menu.xlsm

这是未经测试的,但是它应该使您接近想要的东西。

$Origin = Import-Csv "C:UsersDesktopLogThreat_report.01232014195755.txt"
$Compare = Import-Csv "C:UsersDesktopLogThreat_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
    }
}

查看您在Winmerge中的输入...错别字是否在那里?这些行都不匹配。其中一条线说,阿雷索斯(Aresource)和"匹配"(Match)说。

使用您的新输入,我创建了两个文本文件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数据菜单 $ temp aresource菜单菜单.xlsm

仅在文件2中的事情:

ontap_admin $ vol abcdef groupdata $ central_resource_publish temp数据菜单 $ temp nomenu菜单.xlsmontap_admin $ vol abcdef groupdata $ central_resource_publish temp data菜单 $ temp aresouserce菜单.xlsmontap_admin $ vol abcdef groupdata $ central_resource_publish temp data菜单〜$ temp rsfsesouaferce菜单.xlsm

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

ontap_admin $ vol abcdef groupdata $ central_resource_publish temp data菜单 $ temp rsfSesource菜单菜单.xlsm

相关内容

  • 没有找到相关文章

最新更新