我有这样的输出
v12: "owner" : "cs1372",
v12: "num-of-connections" : 1,
v12: "owner" : "cq5838",
v5: "owner" : "bb9886",
v5: "owner" : "aq0903",
v5: "owner" : "bj2468",
v5: "owner" : "di3080",
v5: "num-of-connections" : 1,
我需要返回连接数行上方的行。所以我想要的输出是
v12: "owner" : "cs1372",
v5: "owner" : "di3080",
有没有一种简单的方法可以使用 sed 或 awk 来做到这一点?
答案1
使用awk
:
$ awk '$2=="\"num-of-connections\""{ print prev } { prev=$0 }' file
v12: "owner" : "cs1372",
v5: "owner" : "di3080",
将当前记录保存在变量中prev
。如果第二个字段等于"num-of-connections"
,则打印变量prev
。