【发布时间】:2023-03-25 04:46:01
【问题描述】:
我一直在尝试使用 SED 完成以下任务,但由于 SED 是基于行的,我恐怕无法做到:
sed -ri 's/(<li>\n\s+<a href="#introduction")/<li><div class="toc-group">Topics<\/div><\/li>\1/' index.php
这应该不会改变:
<li>
<a href="#introduction" class="toc-h1 toc-link" data-title="Introduction">Introduction</a>
进入这个
<li><div class="toc-group">Topics<\/div></li><li>
<a href="#introduction" class="toc-h1 toc-link" data-title="Introduction">Introduction</a>
基本上是穷人的“在特定位置添加一行”。不幸的是\n 没有按预期工作。我认为 AWK 可以做到这一点,但 SED 是我曾经使用过的唯一工具,而且我对 AWK 一无所知,所以请帮助我解决问题。
仅供参考 需要在文档的不同部分添加多个项目,因此我需要一种解决方案,允许我选择代码的一部分,然后在其前面添加。它将在 bash 脚本中用于自动化。
【问题讨论】:
-
用 sed 解析 html 是一个非常糟糕的主意。添加更多上下文数据(当前节点的父节点)
标签: regex shell awk sed replace