5

我有一系列日志文件。

我用 findstr 解析这些日志文件以确定文本字符串的第一个实例,然后返回文件名和找到匹配项的行木材作为变量。

然后我使用 findstr 解析找到匹配项的文件以获取另一个文本字符串。我将匹配的行号作为变量返回。

我现在有了文件,开始和结束行号。

我需要返回行号之间的文本块。

所有输出都重定向到由变量 casenotes 表示的文本文件

这是我的代码:

:test
echo:            >> %casenotes%
echo:   test         >> %casenotes%
for /f "tokens=1,2* delims=:" %%a in ('findstr /N /C:"Optimize ThreadPools" *_MAGT_*.txt') do set startline=%%b & set filefoundin=%%a & goto part2
:part2
for /f "tokens=1,2* delims=:" %%a in ('findstr /N /C:"After optimization" %filefoundin%') do set endline=%%a & goto part3
:part3
echo:                       >> %casenotes%
echo: filefound in: %filefoundin%       >> %casenotes%
echo: startline is: %startline%     >> %casenotes%
echo: endline is:   %endline%       >> %casenotes%
echo:                       >> %casenotes%
     echo: now do something magic to read everything between lines %startline% and %endline% from %filefoundin% and redirect that output to %casenotes%

任何建议表示赞赏!

4

3 回答 3

4

试试这个:@echo off

set file_to_read=read.txt
set /a start_line=1
set /a end_line=6
set outfile=outfile
set counter=1

break > %outfile%
setlocal ENABLEDELAYEDEXPANSION


for /f "delims=*" %%A  in (%file_to_read%) do (
    if !counter! GEQ !start_line! (
        echo %%A
        echo %%A >> !outfile!
    )
    set /A counter=!counter!+1

    if !counter! GEQ !end_line! (
        goto :endLoop
    )
)
:endLoop

这不会计算空行。

于 2012-11-25T19:03:00.293 回答
1
@Echo OFF

Set "File=Test.txt"
Set /A "Start_Line=3"
Set /A "End_Line=6"

For /F "Tokens=*" %%# In ('Type "%FILE%"') DO (
    Call Set /A "Line+=1"
    CMD /C "IF %%LINE%% GEQ %Start_Line% IF %%LINE%% LEQ %END_Line% (Echo %%#>>"Output.txt")"
)

Pause&Exit

更新

另一种具有相同准确性的方法

@Echo OFF

Set    "File=Test.txt"
Set /A "Start_Line=3"
Set /A "End_Line=6"

For /F "Tokens=* delims=" %%# In ('Type "%FILE%"') DO (
    Set /A "Line+=1"
    CALL :READ_LINES "%%#"
)

:READ_LINES
(IF %LINE% GEQ %Start_Line% IF %LINE% LEQ %END_Line% (Echo %*>>"Output.txt") ELSE (GOTO:END)) & GOTO:EOF

:END
Pause&Exit

对于任何其他类型的文本操作,您下次可以使用我的 TEXTMAN 子例程(西班牙语注释行,抱歉):

    @Echo OFF

:: TEXT MANIPULATOR ROUTINE v0.1 
:: by Elektro H@cker

USO:
:TEXTMAN [ACCIÓN] [LÍNEA] [ARCHIVO] [TEXTO (Opcional)]


REM ACCIONES:
REM 
REM  AL  = ADD_LEFT           * AÑADIR TEXTO AL PRINCIPIO DE UNA LÍNEA
REM  AR  = ADD_RIGHT          * AÑADIR TEXTO AL FINAL DE UNA LÍNEA
REM  E   = ERASE              * ELIMINAR UNA LÍNEA
REM  I   = INSERT             * INSERTAR UNA LÍNEA (VACÍA O CON TEXTO)
REM  RL  = REPLACE_LINE       * REEMPLAZAR UNA LÍNEA
REM  RS  = REPLACE_STRING     * REEMPLAZAR PALABRAS DE UNA LÍNEA
REM  RSA = REPLACE_STRING_ALL * REEMPLAZAR PALABRAS EN TODAS LAS LÍNEAS
REM  C+  = CHARACTER_PLUS     * ELIMINAR LOS PRIMEROS "X" CARACTERES EN TODAS LAS LÍNEAS
REM  C-  = CHARACTER_LESS     * ELIMINAR LOS ÚLTIMOS  "X" CARACTERES EN TODAS LAS LÍNEAS
REM  L+  = LINE_PLUS          * CORTAR LAS PRIMERAS "X" LÍNEAS
REM  L-  = LINE_LESS          * CORTAR LAS ÚLTIMAS  "X" LÍNEAS


REM EJEMPLOS DE USO:

:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
::
:: Elimina la línea 3
:: Call :TEXTMAN E 3 "Test.txt"
::
:: Añade una frase al principio de la línea 3
:: Call :TEXTMAN AL 3 "Test.txt" "Elektro H@cker"
::
:: Añade una frase al final de la línea 3
:: Call :TEXTMAN AR 3 "Test.txt" "Elektro H@cker"
::
:: Inserta una línea vacía en la línea 3
:: Call :TEXTMAN I 3 "Test.txt"
::
:: Inserta una línea con texto en la línea 3
:: Call :TEXTMAN I 3 "Test.txt" "Elektro H@cker"
::
:: Reemplaza la línea 3 por completo
:: Call :TEXTMAN RL 3 "Test.txt" "Elektro H@cker"
::
:: Reemplaza las palabras "Elektro" por "H@cker" en la línea 3
:: Call :TEXTMAN RS 3 "Test.txt" "Elektro" "H@cker"
::
:: Reemplaza las palabras "Elektro" por "H@cker" en todas las líneas
:: Call :TEXTMAN RSA "Test.txt" "Elektro" "H@cker"
::
:: Elimina los 3 primeros caracteres de todas las líneas
:: Call :TEXTMAN C+ 3 "Test.txt"
::
:: Elimina los 3 uúltimos caracteres de todas las líneas
:: Call :TEXTMAN C- 3 "Test.txt"
::
:: Elimina las 3 primeras líneas
:: Call :TEXTMAN L+ 3 "Test.txt"
::
:: Elimina las 3 últimas líneas
:: Call :TEXTMAN L- 3 "Test.txt"
::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::

PAUSE&EXIT

:TEXTMAN
(SET /A "A=0", "LINE=0", "TOTAL_LINES=0")  &  (CALL :%~1 %* || (ECHO Parametro incorrecto & Exit /B 1)) & (GOTO:EOF)
:AL
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %~4%%@ >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:AR
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %%@%~4 >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:E
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:I
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF     "%%LINE%%" EQU "%~2" (IF NOT "%~4" EQU "" (Echo %~4 >> "%~nx3.NEW") ELSE (Echo. >> "%~nx3.NEW"))" & Echo %%@ >> "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RL
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (Echo %~4 >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RS
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (Echo %%@ >> "%~nx3.NEW") ELSE (CALL SET "STRING=%%@" && CALL ECHO %%STRING:%~4=%~5%% >> "%~nx3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RSA
(For /F "usebackq tokens=*" %%@ in ("%~2") DO (CALL SET "STRING=%%@" && (CALL ECHO %%STRING:%~3=%~4%% >> "%~2.NEW"))) && (CALL :RENAMER "%~2") & (GOTO:EOF)
:C+
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set   "LINE=%%@" && (CALL ECHO %%LINE:~%~2%% >>    "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:C-
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set   "LINE=%%@" && (CALL ECHO %%LINE:~0,-%~2%% >> "%~nx3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:L+
(Call SET /A "A=%~2-1") && (Call TYPE "%~3" | @MORE +%%A%% > "%~nx3.NEW") && (CALL :RENAMER "%~3") & (GOTO:EOF)
:L-
(FOR /F %%X IN ('TYPE "%~3"') DO (CALL SET /A "TOTAL_LINES+=1")) & (CALL SET /A "TOTAL_LINES-=%~2-1") & (For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set /A "LINE+=1" & Call echo " %%LINE%%!!| FINDSTR " %%TOTAL_LINES%% " && CALL :RENAMER "%~3" && GOTO:EOF || (Echo %%@ >> "%~nx3.NEW")))
:RENAMER
(REN "%~1" "%~nx1.BAK") & (MOVE /Y "%~nx1.BAK" "%TEMP%\" >NUL) & (REN "%~nx1.NEW" "%~nx1") & (GOTO:EOF)
于 2012-11-25T19:30:40.633 回答
1

下面的批处理文件是最快的方法,特别是在文件很大的情况下。

echo: read everything between lines %startline% and %endline% from %filefoundin% and redirect that output to %casenotes%

setlocal EnableDelayedExpansion
set skip=
set /A skiplines=startline-1, numlines=endline-startline+1
if %skiplines% gtr 0 set skip=skip=%skiplines%
(for /F "%skip% delims=" %%a in (%filefoundin%) do (
   echo %%a
   set /A numlines-=1
   if !numlines! equ 0 goto part4
)) > %casenotes%
:part4

如果文件包含感叹号,则需要稍作修改以避免延迟扩展。请,如果您需要,请索取。

请注意,此解决方案会忽略文件中的空行。如果需要,这个细节也可以解决。

安东尼奥

于 2012-11-25T20:38:53.143 回答