Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file findstr到系列输出结果_Batch File - Fatal编程技术网

Batch file findstr到系列输出结果

Batch file findstr到系列输出结果,batch-file,Batch File,我的结果如下: 13:start_index A3 17:index A3 25:start_index A5 29:index A5 13 17 A3 25 29 A5 @echo off setlocal EnableDelayedExpansion REM: have two .txt file like: log.txt and note.txt set errorlogfile=C:\log.txt set notefile=C:\note.txt REM: will be

我的结果如下:

13:start_index A3
17:index A3
25:start_index A5
29:index A5
13 17 A3
25 29 A5
@echo off
setlocal EnableDelayedExpansion

REM: have two .txt file like: log.txt and note.txt

set errorlogfile=C:\log.txt
set notefile=C:\note.txt

REM: will be used file: index.txt and endindex.txt

if exist %indexfile% del /q %indexfile%
if exist %endindexfile% del /q %endindexfile%
set indexfile=C:\index.txt
set endindexfile=C:\endindex.txt

findstr /n start_index %notefile%>%indexfile%
findstr /n index %notefile%|findstr /v start_index>%endindexfile%

for /f "tokens=1,* delims= " %%a in (%indexfile%) do (
    findstr %%b %errorlogfile%>null
    if not errorlevel 1 findstr start_index %indexfile%|findstr %%b
    if not errorlevel 1 findstr index %endindexfile%|findstr %%b
)
13 17 A3
25 29 A5
但我希望它是这样的:

13:start_index A3
17:index A3
25:start_index A5
29:index A5
13 17 A3
25 29 A5
@echo off
setlocal EnableDelayedExpansion

REM: have two .txt file like: log.txt and note.txt

set errorlogfile=C:\log.txt
set notefile=C:\note.txt

REM: will be used file: index.txt and endindex.txt

if exist %indexfile% del /q %indexfile%
if exist %endindexfile% del /q %endindexfile%
set indexfile=C:\index.txt
set endindexfile=C:\endindex.txt

findstr /n start_index %notefile%>%indexfile%
findstr /n index %notefile%|findstr /v start_index>%endindexfile%

for /f "tokens=1,* delims= " %%a in (%indexfile%) do (
    findstr %%b %errorlogfile%>null
    if not errorlevel 1 findstr start_index %indexfile%|findstr %%b
    if not errorlevel 1 findstr index %endindexfile%|findstr %%b
)
13 17 A3
25 29 A5
从一开始,我们有
log.txt
note.txt
,如下所示:

log.txt是:

a
A3
b
c
d
e
A5
f
g
h
start_index A1
a
a
a
index A1

start_index A2
b
b
b
index A2

start_index A3
c
c
c
index A3

start_index A4
d
d
d
index A4

start_index A5
e
e
e
index A5

start_index A6
f
f
f
index A6
note.txt是:

a
A3
b
c
d
e
A5
f
g
h
start_index A1
a
a
a
index A1

start_index A2
b
b
b
index A2

start_index A3
c
c
c
index A3

start_index A4
d
d
d
index A4

start_index A5
e
e
e
index A5

start_index A6
f
f
f
index A6
我的Windows批处理脚本如下所示:

13:start_index A3
17:index A3
25:start_index A5
29:index A5
13 17 A3
25 29 A5
@echo off
setlocal EnableDelayedExpansion

REM: have two .txt file like: log.txt and note.txt

set errorlogfile=C:\log.txt
set notefile=C:\note.txt

REM: will be used file: index.txt and endindex.txt

if exist %indexfile% del /q %indexfile%
if exist %endindexfile% del /q %endindexfile%
set indexfile=C:\index.txt
set endindexfile=C:\endindex.txt

findstr /n start_index %notefile%>%indexfile%
findstr /n index %notefile%|findstr /v start_index>%endindexfile%

for /f "tokens=1,* delims= " %%a in (%indexfile%) do (
    findstr %%b %errorlogfile%>null
    if not errorlevel 1 findstr start_index %indexfile%|findstr %%b
    if not errorlevel 1 findstr index %endindexfile%|findstr %%b
)
13 17 A3
25 29 A5
有没有人有一个好的理想来获得这样的结果:

13:start_index A3
17:index A3
25:start_index A5
29:index A5
13 17 A3
25 29 A5
@echo off
setlocal EnableDelayedExpansion

REM: have two .txt file like: log.txt and note.txt

set errorlogfile=C:\log.txt
set notefile=C:\note.txt

REM: will be used file: index.txt and endindex.txt

if exist %indexfile% del /q %indexfile%
if exist %endindexfile% del /q %endindexfile%
set indexfile=C:\index.txt
set endindexfile=C:\endindex.txt

findstr /n start_index %notefile%>%indexfile%
findstr /n index %notefile%|findstr /v start_index>%endindexfile%

for /f "tokens=1,* delims= " %%a in (%indexfile%) do (
    findstr %%b %errorlogfile%>null
    if not errorlevel 1 findstr start_index %indexfile%|findstr %%b
    if not errorlevel 1 findstr index %endindexfile%|findstr %%b
)
13 17 A3
25 29 A5

这是一个用于此任务的已注释Windows批处理文件,可以在Windows命令提示符窗口中简单地执行,以显示两个文件的所需输出。如果目录
%TEMP%
中尚未存在这两个输入文件,则会创建这两个文件。环境变量
TEMP
可以临时用于执行使用任何目录路径定义的批处理文件

@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem set "TEMP=C:\Temp\Test"

set "ErrorLog=%TEMP%\log.txt"
set "NoteFile=%TEMP%\note.txt"

set "CreatedErrorLog="
set "CreatedNoteFile="
if not exist "%ErrorLog%" call :CreateErrorLog
if not exist "%NoteFile%" call :CreateNoteFile

rem Run FINDSTR searching case-sensitive for all lines containing the either
rem string "index" or string "start_index" at beginning of a line and output
rem those lines with line number to handle STDOUT. An error output to handle
rem STDERR is suppressed by redirecting it to device NUL.

rem The command FOR executes FINDSTR in a separate command process in
rem background and captures the output to STDOUT for processing it line
rem by line.

rem Each line is split up into three substrings using colon after line
rem number and space between keyword and the varying string as delimiters.

rem An environment variable is defined with varying string as name enclosed
rem in # to avoid overwriting a predefined Windows environment variable and
rem make sure later for a 100% string match. The value assigned to this
rem environment variable is the start index line number, the index line
rem number and the varying string from note file. Otherwise the line number
rem of the start index line is assigned to a temporary environment variable.

for /F "tokens=1-3 delims=: " %%A in ('%SystemRoot%\System32\findstr.exe /B /L /N /C:index /C:start_index "%NoteFile%" 2^>nul') do (
    if "%%B" == "index" ( set "#%%C#=!StartIndex! %%A %%C" ) else set "StartIndex=%%A"
)

rem Read each line from error log file without splitting into substrings.
rem If there is an environment variable with string read from file as name
rem enclosed in #, output the value of this environment variable.

for /F "usebackq delims=" %%I in ("%ErrorLog%") do if defined #%%I# echo !#%%I#!

if defined CreatedErrorLog del "%ErrorLog%"
if defined CreatedNoteFile del "%NoteFile%"

rem Restore previous environment and exit this batch file.
endlocal
goto :EOF


:CreateErrorLog
(
echo a
echo A3
echo b
echo c
echo d
echo e
echo A5
echo f
echo g
echo h
) >"%ErrorLog%"
set "CreatedErrorLog=1"
goto :EOF

:CreateNoteFile
(
echo start_index A1
echo a
echo a
echo a
echo index A1
echo/
echo start_index A2
echo b
echo b
echo b
echo index A2
echo/
echo start_index A3
echo c
echo c
echo c
echo index A3
echo/
echo start_index A4
echo d
echo d
echo d
echo index A4
echo/
echo start_index A5
echo e
echo e
echo e
echo index A5
echo/
echo start_index A6
echo f
echo f
echo f
echo index A6
) >"%NoteFile%"
set "CreatedNoteFile=1"
goto :EOF
要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面

  • 呼叫/?
  • del/?
  • echo/?
  • endlocal/?
  • findstr/?
  • 获取/?
  • goto/?
  • 如果/?
  • rem/?
  • 设置/?
  • setlocal/?

另请阅读Microsoft关于的文章,了解
2>nul
的解释。当Windows命令解释器处理此命令行时,重定向运算符
必须使用插入符号
^
上转义,以便在执行命令之前将命令行解释为文字字符,该命令执行嵌入的
findstr
命令行单独的命令过程在后台启动。

不要重复添加不相关的标记。DOS甚至没有
findstr
。它只有
find
。而且它不支持延迟扩展