我是 shell 脚本和 unix 命令的新手,正在尝试找到一种在文件中的模式之间进行惰性匹配的好方法,但无法获得我想要看到的输出。文本类似于以下示例(编辑:修改后的文本字符串
"hello
how
are
you
right
now
and
hey how
you
doing
thanks
see
you
and
ask
me if I
am
doing
okay"
期望的输出:
hey how
you
doing
对于涉及 sed、grep 或 awk 的类似问题,我曾尝试使用我在此处找到的多个答案,但我一直看到的是以下输出:
how
are
you
right
now
and
hey how
you
doing
thanks
see
you
and
ask
me if I
am
doing
我只想在单词“how”和“doing”之间执行惰性匹配并打印两者之间的行(包括)(最好是如何使用解决方案将返回的文本分配给 shell 脚本中的变量)
提前致谢
编辑
最佳答案
I just want to perform a lazy match between the word "how" and "doing" and print the lines in between
你可以使用这个awk
:
awk 'p{s = s ORS $0} /how$/{p=1; s=$0} p && /doing$/{print s; p=0}' file
hey how
you
doing
关于bash - unix bash shell 脚本中的惰性匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59793677/