0

我有这个脚本:

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 (问题区域)中,我正在尝试进行查找和替换以确认该行是我想要的行,并保存该行号。

我该如何编写它才能正确解析变量并正确查找和替换?

我试过的。"[%b]" 表示被替换的 %%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:%%部分%%=% 并使用 ! 似乎从来没有完成任何事情,除了向我展示至少一个。

是的,过了一会儿,我开始乱扔垃圾(很沮丧)。

那么让这个工作的正确方法是什么?

谢谢

4

3 回答 3

3

尝试这个:

@ECHO OFF &SETLOCAL
for /F "delims=:" %%a in ('findstr /N /C:"%SECTION%" "%CHECKER%"') do if not DEFINED StartSection set "StartSection=%%a"
IF NOT DEFINED StartSection (ECHO StartSection %section% NOT found&goto:eof)
for /F "delims=:" %%a in ('MORE +%StartSection% "%CHECKER%" ^|findstr /N /C:"%TARGET%"') do if not DEFINED TargetLine set "TargetLine=%%a"
ECHO StartSection at line #%StartSection%
IF NOT DEFINED TargetLine (ECHO TargetLine %target% NOT found) ELSE ECHO TargetLine at line #%TargetLine%
于 2013-08-04T08:20:44.053 回答
1

打扰一下。我想我不明白你试图用这条线实现什么:

if not %%b==%%b:%SECTION%=% set StartSection=%%a

您必须意识到它%%b包含从文件中读取的,而不是变量!让我们稍微分析一下这个例子:

!%%b:%SECTION%=!

如果%%b将包含一个变量的名称,那么上一行将删除SECTION它的值。但是,%%b不包含变量的名称,而是从文件中读取的行!

如果你想检查从文件中读取的行是否包含 SECTION 给出的字符串,那么你应该这样做:

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 (
        set "line=%%b"
        if not "!line!" == "!line:%SECTION%=!" set StartSection=%%a
    ) else (
. . .
于 2013-08-05T03:39:18.603 回答
0

在 SO 上最难回答的问题是这样的"This doesn't work - how do I fix it?"问题 - 所需内容的唯一线索是 OP 承认的构造不起作用......完全判断所需的东西会磨损 ol' 水晶球 - 和现在的维修成本真的吃掉了蝙蝠的翅膀和蜥蜴的舌头预算……

我也真的无法弄清楚对双重反转的痴迷到底是什么。为什么if not condition thingtwo else thingone??有什么问题if condition thingone else thingtwo?也许我只是过时了——但也许这在政治上是不正确的,或者以某种荒谬的方式具有歧视性。

所以 - 在正在检查的情况下,假设所需的结果是确定第一个dispid 3的行号和包含在文件pushscope中出现的行的行号(其中没有提供样本......)dispid 3%checker%

@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

这对我有用。YMMV。

于 2013-08-04T05:36:59.923 回答