使用批处理DOS提取文本文件的一部分 [英] Extract part of a text file using batch dos

查看:1004
本文介绍了使用批处理DOS提取文本文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,我很感兴趣地只有它的一部分,
一切都包括什么wordA和wordB之间

I have a text file and I'm interested to have only a part of it, all what is included between wordA and wordB

是,可以用一个DOS批处理文件?
非常感谢。

Is that possible using a dos batch file ? many thanks.

推荐答案

下面是一个办法做到这一点时,wordA和wordB不在同一直线上。它将输出发送到output.txt。这将是第一个wordA,并在输入文件中的第一个wordB(区分大小写)之间的文本。你没有指定要怎么做,如果有multple(或不匹配)套wordA / B的。

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.

:RemoveWordB替换wordB与我们并不在文本,我们将然后作为分隔符(`在这种情况下)使用期望的字符。如果需要使用其它字符。

: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.

@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

这篇关于使用批处理DOS提取文本文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆