簡體   English   中英

從第 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啟動目標文件的遞歸目錄列表。

linecount初始化為findstr定位目標字符串。 %%e將設置為找到的行號,將其setlinecount

如果找到字符串, linecount現在將包含一個值,因此請閱讀每一行並倒計時。 如果linecount介於 0 和 -10 之間,我們不會將該行echo顯到輸出文件。

我最初將所有生成的文件放在一個目錄中。 你的隨意改變。

修改為生成一個臨時文件(相當無關緊要的地方),然后將該文件移到原始文件上。

在應用於真實數據之前,請始終根據測試目錄進行驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM