bash - unix bash shell 脚本中的惰性匹配

标签 bash shell awk sed grep

我是 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/

相关文章:

macos - 用于 mac 的 bash shell 脚本从文件生成单词列表?

bash - <<END_SCRIPT 同时通过 bash 连接到 FTP

Shell 脚本变量不为空(-z 选项)

linux - awk 系统语法抛出错误

Linux 脚本 : Passing parameters

linux - 删除模式之间的空行

bash - 通过 grep 管道尾部时没有输出

linux - Bash:cd 到程序输出提供的目录

mysql - 调用 rename() 时 Composer 自更新错误异常 "Permission Denied"

Linux/庆典 : How to unquote?