我有一个大文本文件(大约30,000行),我需要从(行号5-30)中删除一系列行,这是否可以使用批处理?
非常感谢
答案 0 :(得分:1)
批处理可以做到这一点,但会有性能损失,毒性字符可能是一个问题 - 文件的构成可能很重要。
下载GnuSed并运行此命令:
sed 5,30d "file.txt" >"file2.txt"
答案 1 :(得分:0)
如果你想要简单的方法,你可以使用我的例程,这很容易写一个命令:
Call :TEXTMAN SR RANGE_A RANGE_B "Test.txt"
PS:我为你添加了GetRange函数:)
编辑:使用300.000行的文本文件进行测试,不到一秒即可获得结果。
更新2
现在它接受空白行,除非c + c-函数。
的 TextMan.bat 强> 的
@Echo OFF
:: TEXT MANIPULATOR ROUTINE v0.5
:: by Elektro H@cker
REM SYNTAX:
::
:: TEXTMAN [ACTION] [LINE(S)] [FILE] [TEXT]
::
:: * [LINE(S)] parameter is Optional for some actions
:: * [TEXT] parameter is Optional for some actions
REM ACTIONS:
::
:: AB = ADD_BEGINNING * Add text to the beginning of a line.
:: AE = ADD_ENDING * Add text to the end of a line.
:: E = ERASE * Delete a line.
:: I = INSERT * Add a empty line (Or a line with text).
:: RL = REPLACE_LINE * Replace a entire line.
:: RS = REPLACE_STRING * Replace word from line.
:: RSA = REPLACE_STRING_ALL * Replace word from all lines.
:: C+ = CHARACTER_PLUS * Delete the first "X" characters from all lines.
:: C- = CHARACTER_LESS * Delete the last "X" characters from all lines.
:: L+ = LINE_PLUS * Cut the first "X" amount of lines.
:: L- = LINE_LESS * Cut the last "X" amount of lines.
:: GL = GET_LINE * Delete all except "X" line.
:: GR = GET_RANGE * Delete all except "X" range of lines.
REM EXAMPLES:
::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: ::
:: Delete the line 3 ::
:: Call :TEXTMAN E 3 "Test.txt" ::
:: ::
:: Add a string to the beginning of line 3 ::
:: Call :TEXTMAN AL 3 "Test.txt" "Elektro H@cker" ::
:: ::
:: Add a string to the end of line 3. ::
:: Call :TEXTMAN AR 3 "Test.txt" "Elektro H@cker" ::
:: ::
:: Add a empty line at line 3. ::
:: Call :TEXTMAN I 3 "Test.txt" ::
:: ::
:: Add a line with a word at line 3. ::
:: Call :TEXTMAN I 3 "Test.txt" "Elektro H@cker" ::
:: ::
:: Replace the line 3 with "Elektro H@cker". ::
:: Call :TEXTMAN RL 3 "Test.txt" "Elektro H@cker" ::
:: ::
:: Replace the words "Elektro" to "H@cker" in line 3. ::
:: Call :TEXTMAN RS 3 "Test.txt" "Elektro" "H@cker" ::
:: ::
:: Replace the words "Elektro" to "H@cker" in all lines. ::
:: Call :TEXTMAN RSA "Test.txt" "Elektro" "H@cker" ::
:: ::
:: Delete the first 3 characters in all lines. ::
:: Call :TEXTMAN C+ 3 "Test.txt" ::
:: ::
:: Delete the last 3 characters in all lines. ::
:: Call :TEXTMAN C- 3 "Test.txt" ::
:: ::
:: Delete the first 3 lines. ::
:: Call :TEXTMAN L+ 3 "Test.txt" ::
:: ::
:: Delete the last 3 lines. ::
:: Call :TEXTMAN L- 3 "Test.txt" ::
:: ::
:: Delete all except the line number 3. ::
:: Call :TEXTMAN SL 3 "Test.txt" ::
:: ::
:: Delete all except the 3 to 9 range of lines. ::
:: Call :TEXTMAN SR 3 9 "Test.txt" ::
:: ::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:TEXTMAN
(SET /A "A=0", "LINE=0", "TOTAL_LINES=0") & (CALL :%~1 %* || (ECHO Invalid parameter & Exit /B 1)) & (GOTO:EOF)
:AB
(For /F "tokens=1* delims=]" %%A in ('type "%~3" ^| find /n /v ""') DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (if "%%B" EQU "" (Echo+>> "%~3.NEW") ELSE ((Echo %%B)>> "%~3.NEW")) ELSE (if "%%B" EQU "" ((Echo %~4)>> "%~3.NEW") ELSE ((Echo %~4%%B)>> "%~3.NEW"))"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:AE
(For /F "tokens=1* delims=]" %%A in ('type "%~3" ^| find /n /v ""') DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (if "%%B" EQU "" (Echo+>> "%~3.NEW") ELSE ((Echo %%B)>> "%~3.NEW")) ELSE ((Echo %%B%~4)>> "%~3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:E
(For /F "tokens=1* delims=]" %%A in ('type "%~3" ^| find /n /v ""') DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (if "%%B" EQU "" (Echo+>> "%~3.NEW") ELSE ((Echo %%B) >> "%~3.NEW"))"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:I
(For /F "tokens=1* delims=]" %%A in ('type "%~3" ^| find /n /v ""') DO (Call Set /A "LINE+=1" && (CMD /C "IF "%%LINE%%" EQU "%~2" (IF NOT "%~4" EQU "" ((Echo %~4) >> "%~3.NEW") ELSE (Echo+>> "%~3.NEW"))" & (if "%%B" EQU "" (Echo+>> "%~3.NEW") ELSE ((Echo %%B)>> "%~3.NEW"))))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RL
(For /F "tokens=1* delims=]" %%A in ('type "%~3" ^| find /n /v ""') DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (if "%%B" EQU "" (Echo+>> "%~3.NEW") ELSE ((Echo %%B)>> "%~3.NEW")) ELSE ((Echo %~4)>> "%~3.NEW")"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RS
(For /F "tokens=1* delims=]" %%A in ('type "%~3" ^| find /n /v ""') DO (Call Set /A "LINE+=1" && (CMD /C "IF NOT "%%LINE%%" EQU "%~2" (if "%%B" EQU "" (Echo+>> "%~3.NEW") ELSE ((Echo %%B)>> "%~3.NEW")) ELSE (CALL SET "STRING=%%B" && (if "%%B" EQU "" (Echo+>> "%~3.NEW") ELSE ((CALL ECHO %%STRING:%~4=%~5%%)>> "%~3.NEW")))"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:RSA
(For /F "tokens=1* delims=]" %%A in ('type "%~2" ^| find /n /v ""') DO (CALL SET "STRING=%%B" && (if "%%B" EQU "" (Echo+>> "%~2.NEW") ELSE ((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%% >> "%~3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:C-
(For /F "usebackq tokens=*" %%@ in ("%~3") DO (Call Set "LINE=%%@" && (CALL ECHO %%LINE:~0,-%~2%% >> "%~3.NEW"))) && (CALL :RENAMER "%~3") & (GOTO:EOF)
:L+
(Call SET /A "A=%~2") && (Call TYPE "%~3" |@MORE +%%A%% > "%~3.NEW") && (CALL :RENAMER "%~3") & (GOTO:EOF)
:L-
(For /F "tokens=1* delims=]" %%A in ('type "%~3" ^| find /n /v ""') DO (CALL SET /A "TOTAL_LINES+=1")) & (CALL SET /A "TOTAL_LINES-=%~2-1") & (For /F "tokens=1* delims=]" %%A in ('type "%~3" ^| find /n /v ""') DO (Call Set /A "LINE+=1" & Call echo "%%LINE%%"|@FIND "%%TOTAL_LINES%%" >NUL) && (CALL :RENAMER "%~3" && GOTO:EOF) || (Echo %%B >> "%~3.NEW"))
:GL
(Call SET /A "A=%~2" && Call SET /A "A-=1") && (Call TYPE "%~3" |@MORE +%%A%% > "%temp%\getline.tmp") && (For /F "tokens=1* delims=]" %%A in ('type "%temp%\getline.tmp" ^| find /n /v ""') DO ((if "%%B" EQU "" (Echo+>> "%~3.NEW") ELSE ((Echo %%B)> "%~3.NEW"))) && ((CALL :RENAMER "%~3") & (GOTO:EOF)))
:GR
(For /F "tokens=1* delims=]" %%A in ('type "%~4" ^| find /n /v ""') DO (Call Set /A "LINE+=1" && (CMD /C "(IF "%%LINE%%" GEQ "%~2" IF "%%LINE%%" LEQ "%~3" (if "%%B" EQU "" (Echo+>> "%~4.NEW") ELSE ((Echo %%B)>> "%~4.NEW"))) && (IF "%%LINE%%" EQU "%~3" Exit /B 1)" || ((CALL :RENAMER "%~4") & (GOTO:EOF)))))
:RENAMER
(REN "%~1" "%~nx1.BAK") & (MOVE /Y "%~1.BAK" "%TEMP%\" >NUL) & (REN "%~1.NEW" "%~nx1") & (GOTO:EOF)
<强>更新强>
您只需要使用我的例程中的这部分代码:
@Echo OFF
Call :TEXTMAN 5 30 "TextFile.txt"
Pause&Exit
:TEXTMAN
(SET /A "A=0", "LINE=0", "TOTAL_LINES=0") & (CALL :%~1 %* || (ECHO Parametro incorrecto & Exit /B 1)) & (GOTO:EOF)
:GR
(For /F "tokens=1* delims=]" %%A in ('type "%~4" ^| find /n /v ""') DO (Call Set /A "LINE+=1" && (CMD /C "(IF "%%LINE%%" GEQ "%~2" IF "%%LINE%%" LEQ "%~3" (if "%%B" EQU "" (Echo+>> "%~4.NEW") ELSE ((Echo %%B)>> "%~4.NEW"))) && (IF "%%LINE%%" EQU "%~3" Exit /B 1)" || ((CALL :RENAMER "%~4") & (GOTO:EOF)))))
:RENAMER
(REN "%~1" "%~nx1.BAK") & (MOVE /Y "%~1.BAK" "%TEMP%\" >NUL) & (REN "%~1.NEW" "%~nx1") & (GOTO:EOF)
或者你可以一步一步地做到:
如果我们想要范围X到Y(例如5到10)那么......
1 - 将文本文件写入切割前5行的TempFile:
2 - 在TempFile中循环开始向另一个文本文件写入从开始的行,然后在到达“Y”行时停止。
@Echo OFF
Setlocal enabledelayedexpansion
Set /A "X=5"
Set /A "Y=10"
Set /A "Z=%Y%-%X%"
(Type "Test.txt" | MORE +%X%)>"TempFile.txt"
(For /F "Usebackq Tokens=*" %%# in ("TempFile.txt") Do (
Set /A Line+=1
If not !Line! GTR %Z% (Echo %%#) Else (Exit)
))>"MyRange.txt"