2012-11-25 35 views
4

我有一系列日誌文件。如何從第x行讀取文件到第y行(dos/win批處理文件)

我使用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% 

任何意見讚賞!

回答

3

試試這個: 關閉@echo

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 

這將不計空行。

+1

啓用delayedexpansion是一個不好的做法,操作時TEXTFILES(缺字狀或;!) – ElektroStudios

+1

我很抱歉,我應該提到的是有沒有空行中我解析數據文件。接下來會嘗試這個! – user1851547

+1

這工作!種類....它返回我想要的行,但輸出到控制檯而不是我的casenotes文件。 – user1851547

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 

UPDATE

另一種方式與相同的精度

@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子程序的下一次(Commentlines西班牙語,對不起):

@Echo OFF 

:: TEXT MANIPULATOR ROUTINE v0.1 
:: by Elektro [email protected] 

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 [email protected]" 
:: 
:: Añade una frase al final de la línea 3 
:: Call :TEXTMAN AR 3 "Test.txt" "Elektro [email protected]" 
:: 
:: 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 [email protected]" 
:: 
:: Reemplaza la línea 3 por completo 
:: Call :TEXTMAN RL 3 "Test.txt" "Elektro [email protected]" 
:: 
:: Reemplaza las palabras "Elektro" por "[email protected]" en la línea 3 
:: Call :TEXTMAN RS 3 "Test.txt" "Elektro" "[email protected]" 
:: 
:: Reemplaza las palabras "Elektro" por "[email protected]" en todas las líneas 
:: Call :TEXTMAN RSA "Test.txt" "Elektro" "[email protected]" 
:: 
:: 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) 
+0

npocmaka的方法工作更快,但如果你想更準確地嘗試我的方法。 – ElektroStudios

+0

我試過了,它只是掛起... – user1851547

+0

掛起你的電腦?這很奇怪......好吧,我用另一種方法更新了我的答案,具有相同的準確性 – ElektroStudios

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 

如果文件包含驚歎號,需要做一些小修改以避免延遲擴展。請問,如果你需要它的話。

請注意,此解決方案忽略文件中的空行。如果需要,這個細節也可以解決。

安東尼

相關問題