-2
我需要刪除.txt文件中的一個段落,但僅當它包含字符串'Type:Certain'時。有兩條新線,'^ **',以及每段之間的兩條新線。我發現很多關於如何刪除一行的信息,如果它包含某個單詞,但沒有關於某個段落。批處理文件刪除段落,如果它包含某些單詞
我需要刪除.txt文件中的一個段落,但僅當它包含字符串'Type:Certain'時。有兩條新線,'^ **',以及每段之間的兩條新線。我發現很多關於如何刪除一行的信息,如果它包含某個單詞,但沒有關於某個段落。批處理文件刪除段落,如果它包含某些單詞
如果您不害怕正則表達式,則可以使用JREPL.BAT來解決此問題。
您的規格有點不清楚。根據我佈局的理解,我只能假設第2,第6和8應該在下面的示例文本保存:
的test.txt
This is paragraph 1 that should be deleted
xxxx Type: Certain xxxx
End of paragraph 1
^**
This is paragraph 2 that should be preserved
End of paragraph 2
^**
This is paragraph 3 Type:
Certain That should be deleted
End of paragraph 3
^**
This is paragraph 4
Type: Certain That should be deleted
End of paragraph 4
^**
Type: Certain
^**
This is paragrpah 6 that should be preserved
End of paragraph 6
^**
This is paragraph 7 that should be deleted
Type: Certain. End of paragraph 7
^**
This is paragraph 8 that should be preserved
^**
This is paragraph 9 that should be deleted
Type: Certain
下面是使用JREPL解決方案.BAT
可通過分別更改p
和d
的定義來修改段落分隔符和刪除觸發器的正則表達式搜索。
@echo off
setlocal
set "p=\r?\n\r?\n\^\*\*\r?\n\r?\n" %= Paragraph delimiter =%
set "d=\bType:\s+Certain\b" %= Text that indicates deletion =%
set "find=(%p%)?(?:[\s\S](?!%p%))*%d%[\s\S]*?(?:(%p%)|(?![\s\S]))"
set "repl=$txt=($1&&$2)?$1:''"
call jrepl find repl /m /v /jq /f test.txt /o -
這裏是該文件的段落已被刪除後:
This is paragraph 2 that should be preserved
End of paragraph 2
^**
This is paragrpah 6 that should be preserved
End of paragraph 6
^**
This is paragraph 8 that should be preserved
在第一或最後一段Type: Certain
的可能性問題有點複雜。
歡迎來到SO!請編輯你的問題,並提供一些代碼示例 – Blackcoat77
你的問題是脫離主題(因爲你只是要求代碼)並且不清楚(你如何定義一個「段落」?)請閱讀[遊覽]並學習[問]! – aschipfl