gpt4 book ai didi

linux - 如果其中包含一些文本,请删除 html 标签

转载 作者:太空宇宙 更新时间:2023-11-04 11:38:23 25 4
gpt4 key购买 nike

如果 div 的子项与某个字符串匹配,我想删除整个 div。例如:

<div>
some text here
if this text is matched, remove whole div
some other text
</div>

我必须对许多文件执行此操作,因此我正在寻找一些 Linux 命令,例如 sed。

感谢您对此进行调查。

最佳答案

如果我正确理解了你的问题,那么它可以通过一个 sed 命令来实现:

sed '/<div>/I{:A;N;h;/<\/div>/I!{H;bA};/<\/div>/I{g;/\bsome text here\b/Id}}' file.txt

测试

假设这是您的 file.txt:

a. no-div text

<DIV>

some text here
1. if this text is matched, remove whole DIV
some other text -- WILL MATCH
</div>

<div>
awesome text here
2. if this text is matched, remove whole DIV
this will NOT be matched
</div>

b. no-div text

<Div>
another text here
3. if this text is matched, remove whole DIV
and this too will NOT be matched
</Div>

<div>
Some TEXT Here
4. if this text is matched, remove whole DIV
foo bar foo bar - WILL MATCH
</DIV>

c. no-div text

现在,当我运行上面的 sed 命令时,它会给出以下输出:

a. no-div text


<div>
awesome text here
2. if this text is matched, remove whole DIV
this will NOT be matched
</div>

b. no-div text

<Div>
another text here
3. if this text is matched, remove whole DIV
and this too will NOT be matched
</Div>


c. no-div text

正如您可以从上面的输出中验证的那样,只要 some text here 模式在 div 标签之间匹配,那些 div block 已被完全删除。

PS:我在这里进行不区分大小写的搜索,如果您不需要这种行为,请告诉我。我只需要从上面的 sed 命令中删除 I switch。

关于linux - 如果其中包含一些文本,请删除 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5757827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com