相当于 UNIX 差异和补丁的本机 PowerShell [英] Native PowerShell equivalent of UNIX diff and patch

查看:40
本文介绍了相当于 UNIX 差异和补丁的本机 PowerShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在脚本中动态修补文件.

I have a requirement to potentially patch a file on the fly as part of a script.

为了使脚本所做的事情更具可读性,我想以类似于 UNIX差异"和补丁"方法的方式实现这一点.在标准的 UNIX 系统上,diff 可以生成一个特殊格式的文本文件,它表示两个文件之间的差异.这可能会被补丁"与要修改的文件一起使用,以便以干净和直接的方式生成更改后的文件.

In order to make what the script is doing more readable I'd like to implement this in a manner similar to the UNIX "diff" and "patch" approach. On standard UNIX systems diff can generate a specially formatted text file which represents the differences between two files. This may be consumed by "patch" along with the file to modify in order to generate the changed file in a clean and straightforward way.

在 PowerShell 中似乎没有直接的方法可以做到这一点.我见过的选项是使用多行正则表达式 - 这很难构建和解释,消耗整个文件并逐行处理它或将我想要在脚本中包含的新文件编码为 Base64 字符串和只需将其解码为目标文件即可.

It appears that there is no straightforward way to do this in PowerShell. Options I've seen are to use multiple line regular expressions - which are hard to build and to interpret, consuming the whole file and processing it line by line or encoding the new file that I want to have in the script as a Base64 string and simply decoding it to the target file.

虽然 Compare-Object 看起来好像可以做一些事情来生成适当的 diff 对象,但似乎没有简单的方法可以使用 Compare-Object 的输出将更改应用于字符串数组.

While Compare-Object looks as though it could do something to generate an appropriate diff object there appears to be no easy way to use the output of Compare-Object to apply a change to an array of strings.

推荐答案

Compare-Object 是最接近 diff 的东西,没有与 patch 等效的东西 内置于 PowerShell 中.如果您需要该功能,我建议您使用 diffpatch.

Compare-Object is the closest thing to diff, and there's no equivalent to patch built into PowerShell. If you need that functionality I'd recommend using the Windows ports of diff and patch.

这篇关于相当于 UNIX 差异和补丁的本机 PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆