【问题标题】:find the difference of two files and append to the first file powershell查找两个文件的差异并附加到第一个文件 powershell
【发布时间】:2020-06-13 21:35:13
【问题描述】:

file1.txt 有

line1
line2
line3

file2.txt 有

line2
line4
line5
line6

比较并找出差异并写入file3.txt:

Compare-Object (Get-Content file1.txt ) -DifferenceObject (Get-Content file2.txt ) -PassThru | Out-File file3.txt

#append the difference to add to file1:
$From = Get-Content -Path .\file3.txt
Add-Content -Path .\file1.txt -Value $From

当我执行上述操作时,我得到以下输出:

line1
line2
line3line1
line3
line4
line5
line6

file1.txt 中的预期输出是

line1
line2
line3
line4
line5
line6

【问题讨论】:

  • Get-Content -Path .\file*.txt | Select-Object -Unique 怎么样?

标签: powershell


【解决方案1】:

如果文件很小,以下内容适合您

$file1 = Get-Content file1.txt
$file2 = Get-Content file2.txt
$file3 = Compare-Object -ReferenceObject $file1 -DifferenceObject $file2 -PassThru | where SideIndicator -ne '<='

Out-File -InputObject ($file1 + $file3) -FilePath .\file1.txt

【讨论】:

    猜你喜欢
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    相关资源
    最近更新 更多