删除行,直到我使用批处理脚本在文本文件中找到特定行

时间:2016-08-10 07:23:03

标签: batch-file text-files

我有一个文本文件如下:

This is a sample file
some dummy text 
some dummy text
some dummy text
I don't know how many times this some dummy text above me is repeated
I need only the lines starting from here SOME IMP TEXT
SOME IMP TEXT
SOME IMP TEXT
I don't know how many times this SOME IMP TEXT above and below me is repeated
SOME IMP TEXT
I need it till I find this line SOME IMP TEXT
some dummy text
some dummy text
I don't know how many times this some dummy text above me is repeated

我需要一个只包含来自" 的行的文件。我只需要从这里开始的行。一些文字"直到找到" 我需要文字,直到我找到这一行一些文字" (两条粗线包括在内)。我只需要以下内容:

I need only the lines starting from here SOME IMP TEXT
SOME IMP TEXT
SOME IMP TEXT
I don't know how many times this SOME IMP TEXT above and below me is repeated
SOME IMP TEXT
I need it till I find this line SOME IMP TEXT

1 个答案:

答案 0 :(得分:1)

检查tailhead.bat - 它可以显示某个行的文件内容到另一行的编号。如果你有tailHead.bat,你可以:

@echo off
::==========================
:: 
set file=text.txt
set "line1=I need only the lines starting from here SOME IMP TEXT"
set "line2=I need it till I find this line SOME IMP TEXT"
::========================
for /f "tokens=1 delims=:" %%a in ('findstr /n /c:"%line1%" "%file%"') do set "l1=%%a"
for /f "tokens=1 delims=:" %%a in ('findstr /n /c:"%line2%" "%file%"') do set "l2=%%a"

::echo %l1%
::echo %l2%

tailhead.bat -file=text.txt -begin=%l1% -end=%l2%

这将包括线条。如果要排除它们,可以使用set /a

增加开始行并减少结束行