Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于比较文件的Powershell_Powershell - Fatal编程技术网

用于比较文件的Powershell

用于比较文件的Powershell,powershell,Powershell,我是一个新的使用powershell的人。这里我有一个任务来比较两个文件。文件格式如下: 文件A.txt 20160222|LineA 20160222|LineB 20160222|LineC 文件B.txt 20160223|LineE 20160223|LineA 20160223|LineD 20160223|LineB 20130223|LineC 比较之后,我想找出答案 20160223|LineE 20160223|LineD 作为第三个输出文件 我该怎么做呢?这更像是期望的

我是一个新的使用powershell的人。这里我有一个任务来比较两个文件。文件格式如下:

文件A.txt

20160222|LineA
20160222|LineB
20160222|LineC
文件B.txt

20160223|LineE
20160223|LineA
20160223|LineD
20160223|LineB
20130223|LineC
比较之后,我想找出答案

20160223|LineE
20160223|LineD
作为第三个输出文件


我该怎么做呢?

这更像是期望的结果:

$a = Import-Csv .\a.txt -Delimiter "|" -Header 'no', 'line'
$b = Import-Csv .\b.txt -Delimiter "|" -Header 'no', 'line'

$c = Compare-Object -ReferenceObject $a -DifferenceObject $b -Property line


$z =@()
foreach ($d in $c)
{
    if ($d.SideIndicator -eq "=>")
    {
        $z += $b | ?{$_.line -eq $d.line}
    }
    else  # <=
    {
        $z +=$a | ?{$_.line -eq $d.line}
    }
}

这更像是期望的结果:

$a = Import-Csv .\a.txt -Delimiter "|" -Header 'no', 'line'
$b = Import-Csv .\b.txt -Delimiter "|" -Header 'no', 'line'

$c = Compare-Object -ReferenceObject $a -DifferenceObject $b -Property line


$z =@()
foreach ($d in $c)
{
    if ($d.SideIndicator -eq "=>")
    {
        $z += $b | ?{$_.line -eq $d.line}
    }
    else  # <=
    {
        $z +=$a | ?{$_.line -eq $d.line}
    }
}

好吧,这有点复杂,但可以完成任务

# Split the date and value, but keep the raw value intact
$fileA = Get-Content .\fileA.txt | Select-Object -Property @{ Name="Value"; Expression={ $_.Substring($_.IndexOf("|")+1) }}, @{ Name="Raw"; Expression={ $_ }}
$fileB = Get-Content .\fileB.txt | Select-Object -Property @{ Name="Value"; Expression={ $_.Substring($_.IndexOf("|")+1) }}, @{ Name="Raw"; Expression={ $_ }}

# Find the differences, should contain LineE and LineD
$diffs = Compare-Object -ReferenceObject $fileA -DifferenceObject $fileB -Property Value | Select-Object -Expand Value

# Match the diffs against original content and pull out the raw values
$fileB | Where-Object { $diffs -contains $_.Value } | Select-Object -Expand Raw

好吧,这有点复杂,但可以完成任务

# Split the date and value, but keep the raw value intact
$fileA = Get-Content .\fileA.txt | Select-Object -Property @{ Name="Value"; Expression={ $_.Substring($_.IndexOf("|")+1) }}, @{ Name="Raw"; Expression={ $_ }}
$fileB = Get-Content .\fileB.txt | Select-Object -Property @{ Name="Value"; Expression={ $_.Substring($_.IndexOf("|")+1) }}, @{ Name="Raw"; Expression={ $_ }}

# Find the differences, should contain LineE and LineD
$diffs = Compare-Object -ReferenceObject $fileA -DifferenceObject $fileB -Property Value | Select-Object -Expand Value

# Match the diffs against original content and pull out the raw values
$fileB | Where-Object { $diffs -contains $_.Value } | Select-Object -Expand Raw


在比较过程中,似乎忽略了压缩的日期值,这是真的吗?在比较过程中,似乎忽略了压缩的日期值,这是真的吗?结果不是一行一行的。但是,如果每一行还包含分隔符“|”,例如行A=LineA1 | LineA2,行B=LineB1 | LineB2等等,该怎么办。我怎样才能找到20160223 | LineD1 | lineD2和20160223 | LineE1 | line2这是另一回事。您现在应该有一些代码可以根据您的特殊需要进行演变。结果不是LineD和LineE。但是,如果每行还包含分隔符“|”,例如行A=LineA1 | LineA2、行B=LineB1 | LineB2等等,会怎么样。我怎样才能找到20160223 | LineD1 | lineD2和20160223 | LineE1 | line2这是另一回事。您现在应该有一些代码可以根据您的特殊需要进行演变。在这里为前两行抛出错误:“ExpandProperty”需要System.String,$fileA是System.Object[],但如果每一行还包含分隔符“|”,例如行A=LineA1 | LineA2、LineB=LineB1 | LineB2等等,该怎么办。如何查找20160223 | LineD1 | lineD2和20160223 | LineE1 | line2更新为仅在初始
|
上拆分我知道代码的逻辑,但“diff=”行上有错误。因为fileA和fileB是数组。嗯,我似乎对这行没有问题。powershell管道应适当处理阵列。您在该行上收到了什么错误?在这里为前两行抛出错误:“ExpandProperty”需要System.String,$fileA是System.Object[],但如果每行还包含分隔符“|”,例如行A=LineA1 | LineA2、行B=LineB1 | LineB2等等,该怎么办。如何查找20160223 | LineD1 | lineD2和20160223 | LineE1 | line2更新为仅在初始
|
上拆分我知道代码的逻辑,但“diff=”行上有错误。因为fileA和fileB是数组。嗯,我似乎对这行没有问题。powershell管道应适当处理阵列。你在那条线上收到了什么错误?