【问题标题】:compare two files and update the differences to 2nd file比较两个文件并将差异更新到第二个文件
【发布时间】:2015-08-20 00:10:17
【问题描述】:

我正在尝试通过Get-Content 使用 PowerShell 来读取 2 个文件并将文件 1 中的更改更新为文件 2,这是我的代码:

Compare-Object (Get-Content c:\file1) (Get-Content c:file2) | diff > (Get-Content c:file2)

它不起作用,我需要附加文件,以便将任何更改附加到第二个文件。

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0


    【解决方案1】:

    这里有几个问题

    1. 您正在调用 diff,但在 PowerShell 中,这是您从 get-alias diff 看到的 Compare-Object 的别名。我猜这不是预期的。
    2. 如果您想附加第一个文件中出现的差异,您需要相应地过滤来自compare-object 的输出。

    因此,考虑到这一点,我提出......

    $file1 = "c:\file1"
    $file2 = "c:\file2"
    Compare-Object (Get-Content $file1) (Get-Content $file2) | Where-Object{$_.SideIndicator -eq "<="} | Add-Content $file2
    

    $_.SideIndicator -eq "&lt;=" 将只允许 $file1 唯一的条目继续通过管道到 Add-Content。如果您只在Where-Object 之前查看compare-object 的输出,您就可以很好地了解发生了什么。

    【讨论】:

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