使用 awk 或 sed 进行列编辑。如何打印以下图案?

ayr*_*nna 2 solaris sed awk

Sample Input:

title          role        subject
name-JOHN      student      math
school         state        NY
county         street       Phone
name-TOM       student      math
school         state        TX
county         street       Phone
name-LILLY     student      math
school         state        LA
county         street       Phone
name-ROSY      student      math
school         state        WA
county         street       Phone
garbage line 1
garbage line 2
Run Code Online (Sandbox Code Playgroud)

Desired Output

JOHN     NY
TOM      TX
LILLY    LA
ROSY     WA
Run Code Online (Sandbox Code Playgroud)

底部 2 条垃圾线必须消失。我想使用 AWk 还是 SED?

我正在运行 Sun 操作系统。

Cos*_*tas 5

由 GNU sed

sed -n '/^name-/{s///;N;s/[a-z].*\s//p}' file
JOHN      NY
TOM       TX
LILLY     LA
ROSY      WA
Run Code Online (Sandbox Code Playgroud)

由 GNU awk

awk -F'[ -]+' '/name/{a=$2}/state/{print a,$3}' OFS='\t' file
JOHN    NY
TOM     TX
LILLY   LA
ROSY    WA
Run Code Online (Sandbox Code Playgroud)

经过 grep

grep -o '[[:upper:]]\{2,\}' file | paste - -
JOHN    NY
TOM     TX
LILLY   LA
ROSY    WA
Run Code Online (Sandbox Code Playgroud)