首页 > 解决方案 > Less ugly way to use sed to simply include a new line?

问题描述

There are a lot of guides, handbooks, fast-guides, question/answers about it: no one are simple and objective...

It is a classical problem, near all text editors crashes with big files XML or HTML "all in one line", so we need to decide what tag will recive the \n and replace all occurences of <tag by \n<tag ... so simple. Why it is not simple to do by terminal?

The best question/answer about this case not solves: Bash: How can I replace a string by new line in osx bash?
Example using that solution: sed 's/<article/\'$'\n\n<article/g' file.htm not works, need some more exotical syntax, so it is not simple as I solicitated in this question.

So, this quetion is not about "any solution", but about "some simple/elegant solution".

标签: sed

解决方案


如果我了解您在寻找什么,您可以尝试以下方法:

sed 's/<tag>/\n<tag>/g' file.htm

这与您链接的答案非常接近。它在我看来已经很简单了,它用换行符替换标签并再次写入标签。但是,在您的情况下,我不需要这个“$”。


推荐阅读