1
A
回答
1
你可以使用我的例程,我會告訴你,如果一行文本以]
字符開始,但對於簡單/普通文本文件,這可以很好地工作,只是嘗試一下,代碼對於特殊文本文件來說並不完美:
@Echo OFF
:: TEXT MANIPULATOR ROUTINE v0.5
:: by Elektro [email protected]
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 [email protected]" ::
:: ::
:: Add a string to the end of line 3. ::
:: Call :TEXTMAN AR 3 "Test.txt" "Elektro [email protected]" ::
:: ::
:: 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 [email protected]" ::
:: ::
:: Replace the line 3 with "Elektro [email protected]". ::
:: Call :TEXTMAN RL 3 "Test.txt" "Elektro [email protected]" ::
:: ::
:: Replace the words "Elektro" to "[email protected]" in line 3. ::
:: Call :TEXTMAN RS 3 "Test.txt" "Elektro" "[email protected]" ::
:: ::
:: Replace the words "Elektro" to "[email protected]" in all lines. ::
:: Call :TEXTMAN RSA "Test.txt" "Elektro" "[email protected]" ::
:: ::
:: 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)
用法示例插入自定義文本的新行線12:
Call :TEXTMAN I 12 "File.txt" "Text"
0
如果你有Win32的,你可以嘗試腳本EDLIN。
Edlin.script
12
Write new text on line 12
13
Write newtext on line 13
e
命令行
edlin "c:\something\somefile.txt < c:\edlin.script
相關問題
- 1. 使用批處理腳本刪除文件中的特定行
- 2. 批處理腳本刪除文本文件中的特定行
- 3. 集循環在我的批處理腳本打開批處理文件腳本
- 4. 批處理腳本 - 如何寫入文本文件的單行
- 5. 批處理腳本 - 刪除從文本文件的特定行
- 6. 使用批處理腳本每分鐘寫入文本文件
- 7. for循環批處理腳本teminates批
- 8. 使用批處理文件循環訪問Oracle SQL腳本
- 9. 在批處理腳本中使用循環跳過特定的目錄
- 10. 循環的批處理腳本
- 11. 數組的循環批處理腳本
- 12. 使用批處理腳本逐行解析文本文件(批處理文件)
- 13. 批處理文件循環使用FINDSTR
- 14. 批處理打印 - 批處理腳本 - fortr循環中的findstr條件
- 15. 我的批處理文件跳過特定的循環
- 16. FOR批處理文件中的循環
- 17. 批處理腳本中的嵌套循環錯誤處理
- 18. 在批處理腳本中寫入文件不起作用
- 19. 循環從文本輸入的批處理文件
- 20. 如何使用批處理腳本循環遍歷目錄中的文件
- 21. 在批處理腳本中循環遍歷文件名
- 22. 使用批處理文件在批處理文件中的特定行之後插入新行
- 23. 使用用戶輸入值更新文本文件中特定行號的批處理腳本
- 24. 先進for循環批處理腳本
- 25. 批處理腳本:循環和標籤
- 26. 批處理腳本循環變量
- 27. 嵌套for循環 - 批處理腳本
- 28. Windows批處理腳本嵌套循環
- 29. 批處理腳本「繼續」循環?
- 30. Windows批處理文件處理 - 循環
StackOverflow上向能夠提供一些自己的腳本,以增強和修復人面向。如果您想爲您編寫腳本,請嘗試http://www.dostips.com/forum/或http://superuser.com/。 – foxidrive