批处理文件,如果包含某些单词,则删除段落

时间:2017-01-26 15:41:08

标签: batch-file

我需要从.txt文件中删除一个段落,但前提是它包含字符串' Type:Certain'。有两个新行,一个' ^ **'以及每个段落之间的两个新行。我已经找到了很多关于如何删除一行的信息,如果它包含某个单词,但没有关于段落的信息。

1 个答案:

答案 0 :(得分:0)

如果您不害怕正则表达式,可以使用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

的解决方案

可以通过分别更改pd的定义来修改正则表达式搜索段落分隔符和删除触发器。

@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的可能性使问题复杂化。