在 DOS 批处理字符串替换命令中转义等号

发布于 2024-08-25 19:43:53 字数 513 浏览 7 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过潦 2024-09-01 19:43:53

人们不能简单地替换(用)等号(子字符串),而不拆分(使用 "delims==" 的 for 语句)或修剪......

但也许您可以采用这种更简单但更令人困惑的方法,在 for 循环中使用以下语句:

set str=!str:"100M" max-heap-size="%min%M" max-heap-size!

它只是将要替换的字符串组合为 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:

set str=!str:"100M" max-heap-size="%min%M" max-heap-size!

It just combines the string to replace with what comes after instead of what comes before, avoiding any equal-sign replacements entirely.

撩人痒 2024-09-01 19:43:53

最好的解决方案是下载并安装 CygwinGNUWin32 但是,如果您确实仅限于标准命令处理器,那么它可能会变得有点混乱。

这不是世界上最快的方法,但至少是实用的。该命令文件一次处理每一行一个字符,特别处理您找到要查找的节的情况。

@echo off
set init=50M
set max=75M
setlocal enableextensions enabledelayedexpansion
for /f "tokens=* delims=" %%a in (agility.jnlp) do (
    set str1=%%a
    call :morph
    echo !str2!>>agility_new.jnlp
    echo !str2!
)
endlocal
goto :eof

:morph
    set str2=
:morph1
    if not "x!str1!"=="x" (
        if "!str1:~0,18!"=="initial-heap-size=" (
            set str2=!str2!initial-heap-size="!init!"
            set str1=!str1:~24!
            goto :morph1
        )
        if "!str1:~0,14!"=="max-heap-size=" (
            set str2=!str2!max-heap-size="!max!"
            set str1=!str1:~20!
            goto :morph1
        )
        set str2=!str2!!str1:~0,1!
        set str1=!str1:~1!
        goto :morph1
    )
    goto :eof

使用输入文件:

<j2se version="1.5" initial-heap-size="100M" max-heap-size="100M"/>
next line
===

您最终会得到:

<j2se version="1.5" initial-heap-size="50M" max-heap-size="75M"/>
next line
===

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.

@echo off
set init=50M
set max=75M
setlocal enableextensions enabledelayedexpansion
for /f "tokens=* delims=" %%a in (agility.jnlp) do (
    set str1=%%a
    call :morph
    echo !str2!>>agility_new.jnlp
    echo !str2!
)
endlocal
goto :eof

:morph
    set str2=
:morph1
    if not "x!str1!"=="x" (
        if "!str1:~0,18!"=="initial-heap-size=" (
            set str2=!str2!initial-heap-size="!init!"
            set str1=!str1:~24!
            goto :morph1
        )
        if "!str1:~0,14!"=="max-heap-size=" (
            set str2=!str2!max-heap-size="!max!"
            set str1=!str1:~20!
            goto :morph1
        )
        set str2=!str2!!str1:~0,1!
        set str1=!str1:~1!
        goto :morph1
    )
    goto :eof

With the input file:

<j2se version="1.5" initial-heap-size="100M" max-heap-size="100M"/>
next line
===

you end up with:

<j2se version="1.5" initial-heap-size="50M" max-heap-size="75M"/>
next line
===
挽容 2024-09-01 19:43:53

如果您可以将参数作为其他内容(例如双下划线)传递,则可以迭代它们并将它们转换为批处理文件中的“=”。

@rem Replace __ with = in batch files.
@rem This works around the lack of equals signs in args
@rem args contains full args string with substitutions in place

setlocal enabledelayedexpansion

:argloop
if "%~1" NEQ "" (

set str=%~1
set out=!str:__==!
set %~1=!out!
set args=!args!!out!

SHIFT
goto :argloop
)

@rem Can now run program on a line on its own with just %args%

来源: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.

@rem Replace __ with = in batch files.
@rem This works around the lack of equals signs in args
@rem args contains full args string with substitutions in place

setlocal enabledelayedexpansion

:argloop
if "%~1" NEQ "" (

set str=%~1
set out=!str:__==!
set %~1=!out!
set args=!args!!out!

SHIFT
goto :argloop
)

@rem Can now run program on a line on its own with just %args%

Source: https://github.com/mlabbe/batchargs

老街孤人 2024-09-01 19:43:53

这是一个替代解决方案。如果您有能力下载 GNU 工具,则可以使用 sed

C:\test>set a=200
C:\test>sed -i.bak "s/^\(.*initial-heap-size=\"\).*\( max.*\)/\1%a%\"\2/" file 

Here's an alternative solution. If you can afford to download GNU tools, you can use sed:

C:\test>set a=200
C:\test>sed -i.bak "s/^\(.*initial-heap-size=\"\).*\( max.*\)/\1%a%\"\2/" file 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文