可以使用grep来搜索交替模式,即第一个模式A,然后是模式B,然后是模式A,然后是模式B等。
我有一个这样的txt文件:
some lines
some line containing pattern B
some lines
some line containing pattern A
some line containing pattern B
some line containing pattern B
some line containing pattern A
some line containing pattern A
some line containing pattern B
我喜欢用交替模式A和模式B来画线。像这样:
some lines
some line containing pattern B <- Don't take this (no A before)
some lines
some line containing pattern A <- Take
some line containing pattern B <- Take
some line containing pattern B <- Don't take this (no A before)
some line containing pattern A <- Take
some line containing pattern A <- Don't take this (no B before)
some line containing pattern B <- Take
所以我的最终结果是:
some line containing pattern A
some line containing pattern B
some line containing pattern A
some line containing pattern B
答案 0 :(得分:1)
这可以做你想要的。
awk '{m=a>b?a:b;} /pattern A/{a=NR} /pattern B/{b=NR}(m&&m==a&&a<b)||(m&&m==b&&b<a)' awkx
<强>样本强>
lo@ubuntu:~$ awk '{m=a>b?a:b;} /pattern A/{a=NR} /pattern B/{b=NR}(m&&m==a&&a<b)||(m&&m==b&&b<a)' awkx
some line containing pattern A
some line containing pattern B
some line containing pattern A
some line containing pattern B