在txt文件中搜索一行并删除批处理文件中的后续13行

时间:2012-08-27 19:16:01

标签: string text batch-file line

我有一些txt文件需要批处理文件来搜索特定行,然后从txt文件中删除该行和后续的13行。

和txt文件的示例是:

<section1>
line1
line2
line3

<section2>
line1
line2
line3
line4
line5

<section3>
line1
line2
line3
line4
line5
line6
line7
line8

etc.

我需要搜索的行是<section2>,然后删除以下内容:

<section2>
line1
line2
line3
line4
line5

结束于:

<section1>
line1
line2
line3

<section3>
line1
line2
line3
line4
line5
line6
line7
line8

etc.

我还需要在其他部分之后修改一些行,例如在line1中修改<section3>来说editline1

因此它会找到<section3>字符串并将第1行更改为

<section3>
editline1
line2
line3
line4
line5
line6
line7
line8

由于

1 个答案:

答案 0 :(得分:0)

@echo off
  set fnam="%~1"
  set outnam="%~dpn1.out"
  set search4="%~2"
  set section=.
  for /f "tokens=*" %%x in ('type %fnam%') do call :work %fnam% "%%x"
  MOVE /Y %outnam% %fnam%
goto :eof

:work
  set "line=%~2"
  if "%line:~0,1%%line:~-1%"=="<>" (
    if "%section%" neq "." echo.>> %outnam%
    set "section=^%line:~,-1%^>"
    set "line=^%line:~,-1%^>"
  )
  if NOT "%section:~1,-2%>"==%search4% echo %line%>> %outnam%

这样称呼:

section filename.txt "<section2>"