使用命令行中的新GUID替换文件中的所有GUID(Replacing all GUIDs in a file with new GUIDs from the command line)

编程入门 行业动态 更新时间:2024-10-24 14:19:12
使用命令行中的新GUID替换文件中的所有GUID(Replacing all GUIDs in a file with new GUIDs from the command line)

我有一个包含大量出现的字符串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)

最满意答案

我在PowerShell中重写了C#解决方案。 我认为,运行powershell脚本然后编译C#exe会更容易。

使用此步骤的步骤:

下载/安装PowerShell 将代码保存在某处,名为GuidSwap.ps1 修改$ filename和$ outputFilename变量以满足您的需求 运行powershell -noexit c:\ location \到\ guidswap.ps1
## GuidSwap.ps1 ## ## Reads a file, finds any GUIDs in the file, and swaps them for a NewGUID ## $filename = "d:\test.txt" $outputFilename = "d:\test_new.txt" $text = [string]::join([environment]::newline, (get-content -path $filename)) $sbNew = new-object system.text.stringBuilder $pattern = "[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}" $lastStart = 0 $null = ([regex]::matches($text, $pattern) | %{ $sbNew.Append($text.Substring($lastStart, $_.Index - $lastStart)) $guid = [system.guid]::newguid() $sbNew.Append($guid) $lastStart = $_.Index + $_.Length }) $null = $sbNew.Append($text.Substring($lastStart)) $sbNew.ToString() | out-file -encoding ASCII $outputFilename Write-Output "Done"

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:

Download/install powershell Save the code below somewhere, named GuidSwap.ps1 Modify the $filename and $outputFilename variables to suit your needs Run powershell -noexit c:\location\to\guidswap.ps1
## GuidSwap.ps1 ## ## Reads a file, finds any GUIDs in the file, and swaps them for a NewGUID ## $filename = "d:\test.txt" $outputFilename = "d:\test_new.txt" $text = [string]::join([environment]::newline, (get-content -path $filename)) $sbNew = new-object system.text.stringBuilder $pattern = "[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}" $lastStart = 0 $null = ([regex]::matches($text, $pattern) | %{ $sbNew.Append($text.Substring($lastStart, $_.Index - $lastStart)) $guid = [system.guid]::newguid() $sbNew.Append($guid) $lastStart = $_.Index + $_.Length }) $null = $sbNew.Append($text.Substring($lastStart)) $sbNew.ToString() | out-file -encoding ASCII $outputFilename Write-Output "Done"

更多推荐

本文发布于:2023-08-01 02:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   文件   GUID   Replacing   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!