从命令行将文件中的所有 GUID 替换为新的 GUID
我有一个文件包含大量出现的字符串 Guid="GUID HERE"
(其中 GUID HERE
是每次出现的唯一 GUID),我想替换每个现有的 GUID 都有一个新的唯一 GUID。
这是在 Windows 开发计算机上,因此我可以使用 uuidgen.exe 生成唯一的 GUID(每次运行时都会在 stdout 上生成 GUID)。我有 sed 等可用的东西(但奇怪的是没有 awk )。
我基本上想弄清楚是否可以(如果可以,如何)使用命令行程序的输出作为 sed 替换表达式中的替换文本,以便我可以这样做我只需付出最少的努力即可进行更换。我不需要使用 sed - 如果有其他方法可以做到这一点,例如一些疯狂的 vim -fu 或其他程序,那也可以 - - 但我更喜欢使用最少的 *nix 程序集的解决方案,因为我并不真正使用 *nix 机器。
需要明确的是,如果我有一个这样的文件:
etc etc Guid="A" etc etc Guid="B"
我希望它变成这样:
etc etc Guid="C" etc etc Guid="D"
其中 A、B、C、D 当然是实际的 GUID。
(例如,我见过 xargs 用于类似的事情,但它在我需要运行它的机器上也不可用。如果它确实是唯一的方法,我可以安装它,尽管我宁愿不)
I have a file containing a large number of occurrences of the string Guid="GUID HERE"
(where GUID HERE
is a unique GUID at each occurrence) and I want to replace every existing GUID with a new unique GUID.
This is on a Windows development machine, so I can generate unique GUIDs with uuidgen.exe
(which produces a GUID on stdout every time it is run). I have sed
and such available (but no awk
oddly enough).
I am basically trying to figure out if it is possible (and if so, how) to use the output of a command-line program as the replacement text in a sed
substitution expression so that I can make this replacement with a minimum of effort on my part. I don't need to use sed
-- if there's another way to do it, such as some crazy vim
-fu or some other program, that would work as well -- but I'd prefer solutions that utilize a minimal set of *nix programs since I'm not really on *nix machines.
To be clear, if I have a file like this:
etc etc Guid="A" etc etc Guid="B"
I would like it to become this:
etc etc Guid="C" etc etc Guid="D"
where A, B, C, D are actual GUIDs, of course.
(for example, I have seen xargs
used for things similar to this, but it's not available on the machines I need this to run on, either. I could install it if it's really the only way, although I'd rather not)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我在 PowerShell 中重写了 C# 解决方案。我认为运行 powershell 脚本然后编译 C# exe 会更容易。
使用步骤:
I rewrote the C# solution in PowerShell. I figured it would be easier for you to run a powershell script then compile a C# exe.
Steps for using this:
我一直在寻找一种方法来替换 Visual Studio 解决方案中的所有 GUID,因此我获取了 StackOverflow 问题 (GuidSwap.ps1) 的答案并对其进行了扩展,以便脚本跟踪跨多个文件引用的 GUID。下面的标题中显示了一个示例。
I was looking for a way to replace all GUIDs in a Visual Studio solution, so I took the answer to this StackOverflow question (GuidSwap.ps1) and extended it so that the script keeps track of GUIDs that are referenced across multiple files. An example is shown in the header below.
您愿意编译一个 C# 控制台应用程序来执行此操作吗?我很快就把这个搞定了。它将文件名作为命令行参数,查找任何看起来像 GUID 的内容,将其替换为新的 GUID,然后写入文件的新内容。
看一下:
Would you be open to compiling a C# console app to do this? I whipped this up real quick. It takes a filename as a command line argument, finds anything that looks like a GUID, replaces it with a new GUID, and writes the new contents of the file.
Take a look:
您可以先将 uid 捕获到变量中,然后再执行 sed 吗?
you can just capture the uid into a variable first, then do the sed?
我真的很喜欢 BigJoe714 的解决方案。我进一步查找所有特定的扩展文件并替换所有 GUID。
I really like the solution by BigJoe714. I took it one step further finding all specific extension files and replace all GUIDs.