【发布时间】:2016-12-19 05:04:30
【问题描述】:
假设我有一个包含几行的文件:
a1 - first match /a/
b - other stuff
a2 - last match /a/
b
c - first match /c/
c - last match /c/
当我执行awk '/a/,/c/' file时,我会得到
a1 - first match /a/
b - other stuff
a2 - last match /a/
b
c - first match /c/
我想做的是获得/a/和/c/之间的中间部分。但是模式 /a/ 和 /c/ 匹配了几行,并且在这些行之间还有一些其他的东西。所以我想知道是否有一种简单的方法可以得到这样的结果:
a2 - last match /a/
b
c - first match /c/
【问题讨论】:
-
这里的逻辑是什么?你还有其他例子吗? a 和 c 总是出现两次吗?可以有多块吗?
-
开始模式和停止模式匹配几行,但我只想要中间部分。假设我们有
a\na\na\nb\nc\nc\nc\n,我想得到a\nb\nc\n -
管道输出到
uniq以消除重复。 -
但是如果 "ababcc" 到 "abc" @Barmar
-
不确定这意味着什么。当我读到这个问题时,看起来你想摆脱
a和c的重复。您应该编辑问题以解释您真正想要的内容,而不仅仅是在评论中进行说明。
标签: regex shell awk pattern-matching