Findstr到系列输出结果



我有这样的结果:

13:start_index A3
17:index A3
25:start_index A5
29:index A5

但我想要这样的:

13 17 A3
25 29 A5

从开始时,我们有log.txtnote.txt,如下所示:

log.txt 是:

a
A3
b
c
d
e
A5
f
g
h

note.txt 是:

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批处理脚本是这样的:

@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:TempTest"
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%System32findstr.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

用于了解使用的命令及其工作方式,打开命令提示符窗口,执行以下命令,并完全仔细阅读所有命令显示的所有帮助页面。

  • call /?
  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

还请阅读有关使用命令重定向操作员的2>nul说明的Microsoft文章。重定向运算符>必须使用上的 ^命令行上逃脱为命令行,当windows命令命令解释器处理此命令行之前,在执行命令之前,要执行执行该命令行。嵌入式findstr命令行使用使用单独的命令过程在后台开始。

最新更新