0

我有一些 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

谢谢

4

1 回答 1

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>"
于 2012-08-27T20:43:05.237 回答