使用子字符串替换cmd中的文本

kee*_*han 2 powershell cmd batch-file

我需要打开一个文件并搜索子字符串。然后替换同一行的其他文本。

必须搜索文本:#define MyAppVersion并替换双引号内的版本(这是动态的,将从不同的文件中获取)。

#define MyAppVersion "1.0.0"
Run Code Online (Sandbox Code Playgroud)

所以这就是我目前所拥有的:

rem version num to be retrieved elsewhere, set here for simplicity on sample
set VER=1.1.0
powershell -Command "(gc sample.iss) -replace '#define MyAppVersion', '#define MyAppVersion "%VER%"' | Out-File sample.iss"
Run Code Online (Sandbox Code Playgroud)

但这只会输出这样的结果:

#define MyAppVersion 1.1.0 "1.0.0"
Run Code Online (Sandbox Code Playgroud)

那么如何在不使用任何第三方插件的情况下更改整行?

谢谢!

kee*_*han 5

终于可以发挥作用了

set VER=1.1.0
powershell -Command "(gc sample.iss) -replace '#define MyAppVersion.+', '#define MyAppVersion "\"%VER%"\"' | Out-File mod.iss"
Run Code Online (Sandbox Code Playgroud)

.+匹配该行的其余部分