另一个解决方案,只是为了好玩:
[ ~]$ awk 'BEGIN{b=e=0; s=es=""}
($0 ~ "^zzz.*"){b=1}
($0 ~ ".*test$"){e=1; b=0; es=s; s=""; if(es!=""){es=es"\n"$0}else{es=$0}}
(b==1){if(s!=""){s=s"\n"$0}else{s=$0}} END {print es}' file
具有相同输入文件的输出:
zzz and ssss
this is for testing
so test
如果您将 ".*test$" 更改为与输入文件中的单词不对应的另一个模式,则此命令将不会产生输出:
[ ~]$ awk 'BEGIN{b=e=0; s=es=""}
($0 ~ "^zzz.*"){b=1}
($0 ~ ".*jjj$"){e=1; b=0; es=s; s=""; if(es!=""){es=es"\n"$0}else{es=$0}}
(b==1){if(s!=""){s=s"\n"$0}else{s=$0}} END {print es}' file
[ ~]$
当然,您可以使用“-v”选项使正则表达式易于配置。
否则,带有 grep 的 anubhava 的提议不适用于我的笔记本电脑:
[neumann@MacBookPro ~]$ cat file
this is for testing
so test
againa and again
zzz and ssss
this is for testing
so test
againa and again
[neumann@MacBookPro ~]$ grep -oP 'zzz[\s\S]*test' file
[neumann@MacBookPro ~]$ grep --version
grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
[neumann@MacBookPro ~]$
这就是为什么当我有一个包含多行的模式时,我使用 awk 来执行此操作。