【发布时间】:2014-10-21 20:35:12
【问题描述】:
File:
this is a paragraph
to find in another
file
some stuff ..
more stuff ...
this is a paragraph
to find in another
file
more stuff ...
another paragraph
to match
yet more stuff..
this is a paragraph
duplicate in this
file
another paragraph
to match
this is a paragraph
duplicate in this
file
yet more stuff..
this is a paragraph
to find in another
file
应该返回:
this is a paragraph
to find in another
file
some stuff ..
more stuff ...
more stuff ...
another paragraph
to match
yet more stuff..
this is a paragraph
duplicate in this
file
yet more stuff..
我找到了 pcregrep -n -M,我知道我可以使用 sed 和这个命令循环搜索每个段落,但是 pcregrep 并不是在每个系统上都有,所以如果可以避免它会很好。使用标准的 *nix 东西寻找优雅的东西,最好不要使用 perl。
* 一些不错的帖子和想法,但尽管在我发布的有限案例中确实有效,但它们并没有普遍发挥作用,因此我调整了示例数据,以便您查看它是否会更普遍
* 这是一个只打印多行段落的 sed 单行:
sed -e '/./{H;$!d;}' -e 'x;/.*\n.*\n.*/!d' file
【问题讨论】:
-
我打印了错误的输出,但由于上次输出,它仍然无法正常工作,最新版本可以: awk -v RS= '{gsub(/[[:blank:]]+ $/,""); gsub(/[[:blank:]]+\n/,"\n")} !seen[$0]++{ORS=RT;print}' - 谢谢