使用 Windows 批处理提取文本文件的一部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15042909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extract part of a text file using Windows batch
提问by user2102732
I have a text file and I'm interested to have only a part of it, all that is included between wordA and wordB
我有一个文本文件,我只想拥有其中的一部分,所有内容都包含在 wordA 和 wordB 之间
Is that possible using a cmd batch file?
可以使用 cmd 批处理文件吗?
回答by RGuggisberg
Here's one way to do it. Assuming that wordA and wordB are on the same line (both must be present). The subroutine :FindString removes undesired preceding text and then replaces "wordB" with a character that we don't expect in the text that we will then use as a delimiter (` in this case). use another character if necessary.
这是一种方法。假设 wordA 和 wordB 在同一行(两者都必须存在)。子程序 :FindString 删除不需要的前面的文本,然后用我们不希望在文本中用作分隔符的字符替换“wordB”(在这种情况下为`)。如有必要,请使用另一个字符。
@ECHO OFF
SET InFile=Test.txt
FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
pause
GOTO :eof
:FindString
SET String=%~1
SET String=%String:*wordA =%
SET String=%String: wordB=`%
FOR /F "tokens=1 delims=`" %%A IN ('ECHO.%String%') DO ECHO.%%A]
GOTO :eof
回答by RGuggisberg
Here's a way to do it when "wordA" and "wordB" are not on the same line. It sends output to Output.txt. That will be the text between the first "wordA" and the first "wordB" in the input file (case sensitive). You didn't specify what to do if there are multple (or mismatched) sets of wordA/B.
当“wordA”和“wordB”不在同一行时,这是一种方法。它将输出发送到 Output.txt。这将是输入文件中第一个“wordA”和第一个“wordB”之间的文本(区分大小写)。如果有多个(或不匹配的)wordA/B 集,您没有指定要做什么。
:RemoveWordB replaces "wordB" with a character that we don't expect in the text that we will then use as a delimiter (` in this case). use another character if necessary.
:RemoveWordB 将“wordB”替换为我们不希望在文本中用作分隔符的字符(在本例中为`)。如有必要,请使用另一个字符。
@ECHO OFF
SET InFile=Test.txt
SET OutFile=Output.txt
IF EXIST "%OutFile%" DEL "%OutFile%"
SET TempFile=Temp.txt
IF EXIST "%TempFile%" DEL "%TempFile%"
FOR /F "tokens=*" %%A IN ('FINDSTR /N "wordA" "%InFile%"') DO (
CALL :RemovePrecedingWordA "%%A"
FOR /F "tokens=1 delims=:" %%B IN ('ECHO.%%A') DO (
MORE +%%B "%InFile%"> "%TempFile%"
FINDSTR /V "wordB" "%TempFile%">> "%OutFile%"
FOR /F "tokens=*" %%C IN ('FINDSTR "wordB" "%InFile%"') DO (
CALL :RemoveWordB "%%C"
IF EXIST "%TempFile%" DEL "%TempFile%"
GOTO :eof
)
)
)
GOTO :eof
:RemovePrecedingWordA
SET String=%~1
SET String=%String:*wordA =%
ECHO.%String%> "%OutFile%"
GOTO :eof
:RemoveWordB
REM Replace "wordB" with a character that we don't expect in text that we will then use as a delimiter (` in this case)
SET LastLine=%~1
SET LastLine=%LastLine:wordB=`%
FOR /F "tokens=1 delims=`" %%A IN ('ECHO.%LastLine%') DO ECHO.%%A>> "%OutFile%"
GOTO :eof
回答by foxidrive
This uses a helper batch file called repl.bat
from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
这使用repl.bat
从 - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855调用的帮助程序批处理文件
Put repl.bat
in the same folder as the batch file.
放在repl.bat
与批处理文件相同的文件夹中。
@echo off
type infile |repl ".*wordA(.*)wordB.*" "" >outfile