从文件的第 N 行批量删除 X 行

Batch removing X number of lines from file from Nth line

我正在尝试从一组 html 文件中删除标签,但我似乎找不到正确的命令来完成这项工作。

我能找到标签的行号;该标签总共有10行需要删除。找到这一行后,如何获取并删除该行和接下来的 10 行?

这是我的(第一个 for 循环收集文件,第二个 for 循环收集所有行号。)谢谢。

::start by creating a list of html files
set i=0
for /r %%G in (*.html) do (
    set /A i+=1
    set array[!i!]=%%G
)
set n=%i%

::second nested loop collects all lines for bottom secion to be deleted.
set j = 0
for /L %%i in (1,1,%n%) do (
     for /f "delims=" %%a in ('findstr /n /c:"u-backlink u-clearfix u-grey 80" !array[%%i]!') do (
         set var=%%a
         set /A j+=1
         set array[!j!]=!var:~0,3!
     )
)
set m=%j%
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory & destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"

for /r "%sourcedir%" %%G in (*.html) do (
 SET "linecount="
 FOR /f "delims=:" %%e IN ('findstr /n /c:"u-backlink u-clearfix u-grey 80" "%%G"') DO SET /a linecount=%%e
 IF DEFINED linecount (
  FOR /f "usebackqdelims=" %%b IN ("%%G") DO (
   SET /a linecount-=1
   IF !linecount! gtr 0 ECHO %%b
   IF !linecount! lss -10 ECHO %%b
  )
 )>"%destdir%\tempfile"& MOVE /y "%destdir%\tempfile" "%%G" >nul
)
GOTO :EOF

sourcedir.

开始目标文件的递归目录列表

初始化linecountnothing并使用findstr定位目标字符串。 %%e会被设置为找到的行号,set这个变成linecount.

如果找到字符串,linecount 现在将包含一个值,因此请阅读每一行并倒数。如果 linecount 介于 0 和 -10 之间,我们不会 echo 输出文件的行。

我本来是把所有生成的文件都放在一个目录下的。随意换。

已修改以生成一个临时文件(相当 其中 是无关紧要的)然后将该文件移到原始文件上。

在应用到实际数据之前,始终对照测试目录进行验证。