如何为此使用单个sed命令?

如何为此使用单个sed命令?,sed,Sed,我有一个问题: 使用单个SED命令转换以下文本: <h1>this is the h1 header</h1> <h2>this is the h2 header</h2> <h3>this is the h3 header</h3> <h4>this is the h4 header</h4> 对本案文: <div>this is the h1 header</div&

我有一个问题:

使用单个SED命令转换以下文本:

<h1>this is the h1 header</h1>

<h2>this is the h2 header</h2>

<h3>this is the h3 header</h3>

<h4>this is the h4 header</h4>
对本案文:

  <div>this is the h1 header</div>

  <div>this is the h2 header</div>

  <div>this is the h3 header</div>

  <div>this is the h4 header</div>
这是我提出的答案,但有没有一种方法可以在一个命令中实现这一点? f3是我的文件名

sed -e 's/<h.>/<div>/g' -e 's/<\/h.>/<\/div>/g' f3
你可以用

sed 's/h[1-4]/div/g' f3

不客气,谢谢你的编辑。虽然我知道你不想要2个或更多的-e,但请记住,这实际上是一个单一的命令。因此,在我再次使用关键字“sed”之前,它只是一个单一的命令,对吗?对,它只是一个。我建议使用XML/HTML解析器xmlstarlet,xmllint…@Cyrus它与HTML无关,只是教授给了我们这个sed命令的实践文档。这一个有效,但为什么我们在sed命令中使用pipe?它有什么用途?我们看到最多的分隔符是/,但几乎没有任何字符是有效的。这里的优点是我们不必逃避/in/div。
sed 's/h[1-4]/div/g' f3