linux - 打印文件中 "End"关键字之间的行

标签 linux unix awk sed

从第一次出现“End”到最后一次出现“End”,需要grep之间的所有行

在文件中 grep End 关键字之间的行

考虑包含以下行的输入文件

one
two
----End-----
two
three
---End-----
four
ten
---End-----
six
seven

预期输出:

----End-----
two
three
---End-----
four
ten
---End-----

最佳答案

$ awk '
/End/ {                        # at End
    print (b==""?"":b ORS) $0  # print buffer and End line
    f=1                        # flag up
    b=""                       # reset buffer
    next
}
f {                            # after first End 
    b=b (b==""?"":ORS) $0      # gather the buffer
}' file
----End-----
two
three
---End-----
four
ten
---End-----

关于linux - 打印文件中 "End"关键字之间的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46026206/

相关文章:

linux - 用于汇总文件列的 awk 命令

linux - 不包含字符串的处理行和匹配行下方的输出行

linux - Git 错误 : fatal: protocol error: bad line length character: git@

go - Go 内部如何处理 os.Signal channel ?

linux - 如何用shell函数制作 "dictionary"?

c - (在 UNIX 中执行内置程序(C)

bash - AWK 寻找一种方法来打印包含逗号分隔字符串中的单词的行

php - 将更改上传到模板文件时 Laravel 错误 500

linux - 我想在没有硬件的情况下运行基于 Linux 的设备应用程序

PHP CURL(7.48.0) DNS解析在/etc/hosts