简体   繁体   English

Windows命令行在括号之间查找并替换(repl.bat)

[英]Windows command line find and replace between brackets (repl.bat)

I have a variable DATE, which contains something like 06092017 I have a file with the following line: 我有一个变量DATE,其中包含类似06092017的内容,我有一个带有以下行的文件:

#define BUILDDATE (05092017)

where the value between the brackets changes 括号之间的值发生变化的地方

I'm trying to use this to find and replace the text: 我正在尝试使用它来查找和替换文本:

type "file.h"|repl "(#define BUILDDATE \().*?(\))" "$1%DATE%$2" >"file_new.h"

But nothing has changed in file_new.h . 但是file_new.h没有任何变化。 What am I doing wrong please? 请问我做错了什么? Or is there a better way? 或者,还有更好的方法?

Why use a external script at all? 为什么要完全使用外部脚本?

@echo off

REM Prepare example to make batch file self-contained (this will overwrite file.h!)
echo.#define whatever              > file.h
echo.#define BUILDDATE (05092017) >> file.h
echo.#define something (1234)     >> file.h

for /F "usebackq tokens=1,2,*" %%A in (`type "file.h"`) do @if "%%~A"=="#define" if "%%~B"=="BUILDDATE" (
    for /F "tokens=1 delims=() " %%X in ("%%~C") do (
        echo.%%X > "file_new.h"
    )
)

echo Result:
type "file_new.h"

Thanks guys. 多谢你们。 Just noticed that in my file.h, there was a tab between BUILDDATE and the brackets rather than a space that was causing it to fail. 刚注意到,在我的file.h中,BUILDDATE和方括号之间有一个制表符,而不是导致它失败的空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM