如何格式化找到替换里面循环变量 [英] How to format find-replace variables inside for loop
问题描述
我有这样的脚本:
setlocal EnableDelayedExpansion
set SECTION=dispid 3
set TARGET=pushscope
@echo on
for /F "tokens=1* delims=:" %%a in ('findstr /N /C:"%SECTION%" /C:"%TARGET%" %CHECKER%') do (
if not defined StartSection (
if not %%b==%%b:%SECTION%=% set StartSection=%%a
) else (
goto :EOF
if not defined TargetLine (
goto :EOF :: Do nothing yet
if not %%b equ !%%b:%TARGET%=! set TargetLine=%%a& goto checkercont
)
)
)
在StartSection(问题区域),我试图做一个查找和替换,以确认这条线就是我的愿望,并保存行号。
In StartSection (the problem area), I'm trying to do a find and replace to confirm that this line is the one I desire, and save that line number.
我如何写它,以便它将正确解析变量并做查找和替换正确?
How do I write it so that it will parse the variables correctly and do the find and replace properly?
我已经试过。 [%B]是指代替%%乙:
What I've tried. "[%b]" means the replaced %%b:
%%b:%SECTION%=% returns "[%b]:dispid 3=a" (Yes, it somehow gives me an 'a').
!%b:%SECTION%=! returns "!SECTIONa"
!%%b:%SECTION%=! returns "![%b]:dispid 3=!" (which then gives me an error saying 3 is not a command)
!%b:%SECTION%=%! returns "!SECTION!"
!%%b:!SECTION!=! returns "![%b]:!SECTION!=!"
%b:%%SECTION%%=% return "dispid 3" (and error)
我觉得像它解析大胆那么斜体最后一个。 %B:%的 %SECTION%的 的%=%
并使用!似乎从来没有完成,除了显示至少有一个回来给我任何东西。
I feel like for the last one that it parses the bold then the italics. %b:%%SECTION%%=% And using ! never seems to accomplish anything except display at least one back to me.
是的,我刚开始在那里乱抛垃圾一点点后(很沮丧)。
And yes, after a little bit I just started throwing random crap in there (got frustrated).
那么,什么是得到这个工作的正确方法?
So what's the proper way to get this working?
感谢
推荐答案
最难的类型问题SO回答的是这不工作 - 如何解决呢
的问题 - 其中唯一的线索所需要的是,该OP承认不工作的构建......占卜十分什么是需要相当磨损醇'水晶球 - 和维修成本时下真是吃成蝙蝠的翅膀和蜥蜴的舌头预算...
The hardest type of question to answer on SO is the "This doesn't work - how do I fix it?"
question - where the only clue to what is required is a construct that the OP admits isn't working... Divining quite what is required quite wears out the ol' crystal ball - and repair costs nowadays really eats into the bats' wings and lizards' tongues budget...
我实在想不出有什么用双反转的痴迷是一回事,无论是。为什么如果没有别的调理thingtwo thingone
??这有什么错 if条件thingone其他thingtwo
?也许我只是老式的 - 但也许它在一些荒谬的方式政治上不正确或dicriminatory
I really can't figure out what the obsession with double-inversion is all about, either. Why if not condition thingtwo else thingone
?? What's wrong with if condition thingone else thingtwo
? Maybe I'm just old-fashioned - but perhaps it's politically incorrect or dicriminatory in some absurd way.
所以 - 在被检查的情况下,假设所期望的结果是确定所述第一 DISPID 3
和包含该行的行号 pushscope
该文件%检查%
(在后 DISPID 3
出现其中没有样品提供...)
So - in the case under examination, assuming the desired result is to determine the linenumber of the first dispid 3
and that of the line containing pushscope
that appears after the dispid 3
in the file %checker%
(of which no sample is provided...)
@ECHO OFF
SETLOCAL
set SECTION=dispid 3
set TARGET=pushscope
SET checker=sourcefile.txt
SET startsection=
SET targetline=
for /F "tokens=1* delims=:" %%a in ('findstr /N /C:"%SECTION%" /C:"%TARGET%" %CHECKER%') do (
IF DEFINED startsection (
IF NOT DEFINED targetline (
ECHO %%b|FIND "%target%">NUL
IF NOT ERRORLEVEL 1 SET targetline=%%a
)
) ELSE (
ECHO %%b|FIND "%section%">NUL
IF NOT ERRORLEVEL 1 SET startsection=%%a
)
)
ECHO startsection=%startsection% targetline=%targetline%
GOTO :EOF
这对我的工作。情况因人而异。
Which worked for me. YMMV.
这篇关于如何格式化找到替换里面循环变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!