在 DOS 批处理字符串替换命令中转义等号
我需要使用 DOS 批处理文件替换 JNLP 文件中的一些文本,以针对本地计算机进行调整。
问题在于搜索模式包含一个等号,这会扰乱批处理文件中的字符串替换。
我想
<j2se version="1.5" initial-heap-size="100M" max-heap-size="100M"/>
用初始和最大堆大小的特定设置来替换该行。
例如,目前我有,
for /f "tokens=* delims=" %%a in (%filePath%agility.jnlp) do (
set str=%%a
set str=!str:initial-heap-size="100M"=initial-heap-size="%min%M"!
echo !str!>>%filePath%new.jnlp)
但搜索模式中的 = 被读取为替换命令的一部分。
如何转义等号以便将其作为文本处理?
I need to replace some text in a JNLP file using a DOS batch file to tune it for the local machine.
The problem is that the search pattern contains an equals sign which is messing up the string replacement in the batch file.
I want to replace the line,
<j2se version="1.5" initial-heap-size="100M" max-heap-size="100M"/>
with specific settings for the initial and max heap sizes.
For example at the moment I have,
for /f "tokens=* delims=" %%a in (%filePath%agility.jnlp) do (
set str=%%a
set str=!str:initial-heap-size="100M"=initial-heap-size="%min%M"!
echo !str!>>%filePath%new.jnlp)
but the = in the search pattern is being read as part of the replacement command.
How do I escape the equals sign so it is processed as text?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
人们不能简单地替换(用)等号(子字符串),而不拆分(使用
"delims=="
的 for 语句)或修剪......但也许您可以采用这种更简单但更令人困惑的方法,在 for 循环中使用以下语句:
它只是将要替换的字符串组合为 after 后面的内容,而不是 before 后面的内容,完全避免任何等号替换。
One cannot simply replace (a substring with) an equal-sign, without splitting up (for-statement with
"delims=="
) or trimming…But perhaps you could go for this simpler but more confusing approach, using the following statement in your for-loop:
It just combines the string to replace with what comes after instead of what comes before, avoiding any equal-sign replacements entirely.
最好的解决方案是下载并安装 Cygwin 或 GNUWin32 但是,如果您确实仅限于标准命令处理器,那么它可能会变得有点混乱。
这不是世界上最快的方法,但至少是实用的。该命令文件一次处理每一行一个字符,特别处理您找到要查找的节的情况。
使用输入文件:
您最终会得到:
The best solution is to download and install Cygwin or GNUWin32 but, if you're really limited to the standard command processor, it can get a little messy.
This is not the fastest method in the world but it's at least functional. This command file processes each line one character at a time, treating specially the case where you find the stanza you're looking for.
With the input file:
you end up with:
如果您可以将参数作为其他内容(例如双下划线)传递,则可以迭代它们并将它们转换为批处理文件中的“=”。
来源:https://github.com/mlabbe/batchargs
If you can pass the arguments as something else, such as double underscores, you can iterate through them and convert them to '=' in the batch file.
Source: https://github.com/mlabbe/batchargs
这是一个替代解决方案。如果您有能力下载 GNU 工具,则可以使用 sed:
Here's an alternative solution. If you can afford to download GNU tools, you can use sed: