使用标准的 unix 命令,你如何抓取一堆行?

Utk*_*nha 2 unix alias

我有几个这种格式的文本文件:

name: john
address: bay area
phone: 6505561234
image: /work/myimage.png

name: stark
dob: 5AD
shirt color: red
physical address: Westros
phone model: S2
email id: me@stark.org

phone model: S2
name: tara
dob: 1ad
shirt color: red
physical address: Westros
email id: me@stark.org
Run Code Online (Sandbox Code Playgroud)

可以有多个“人”或“联系人”。假设我想找到所有使用“S2”手机型号的人。

我可以做一个“grep”——那只会返回这个:

phone model: S2
phone model: S2
Run Code Online (Sandbox Code Playgroud)

我可以在 grep 上使用 before/after 上下文 - 但这只是打印出来之前/之后的固定行数。使用 3 的“前一个”上下文,我可能会得到这样的信息:

shirt color: red
physical address: Westros
phone model: S2
---
name: tara
dob: 1ad
phone model: S2
Run Code Online (Sandbox Code Playgroud)

但这不是我想要的。我希望显示整个“记录”。有关如何使用标准 unix 命令执行此操作的任何线索?

ave*_*ian 5

awk 'BEGIN {RS="\n\n"} $0 ~ /PATTERN/ {print $0"\n---"}' record

只需用你想要的任何东西替换 PATTERN 。