我想匹配"sid"
for VALUE1
(= 789069
)之后的值
问题是行的顺序是随机的。
[{"cid":"PWER","data":[{"VALUE1":0}],"sid":"789069","units":"kWm","age":586667},
{"cid":"PWER","data":[{"VALUE2":809}],"sid":"788325","units":"kWm","age":11},
{"cid":"PWER_SUB","data":[{"VALUE3":278}],"sid":"789540","units":null,"age":1},
{"cid":"PWER_SUB","data":[{"VALUE4":319}],"sid":"789093","units":null,"age":38},
{"cid":"PWER_SUB","data":[{"VALUE5":0}],"sid":"789069","units":null,"age":4}
有什么想法如何搭配吗?
答案1
答案2
Grep 是解决此类问题的好工具
grep -oP '"sid":"\d+"' file
。这是你想要的?
更新: 它不是!!! (抱歉:我没听懂最初的问题。)
第二个版本:使用名为的 json 工具json
:
cat a.json | json -c '"VALUE1" in this.data[0]' | json -a sid
或者
cat a.json | json -c '"VALUE1" in this.data[0]' -a sid
json -c predicate
- 过滤元素(过滤器)json -a expression
- 将表达式应用于所有元素(地图)
文档位于:http://trentm.com/json/。如果没有安装:
install node
and sudo npm install -g json
答案3
如果awk
你想匹配整个记录,那么你可以使用:
awk '{if($0~/789069/){print $0}}' file
在此代码中,awk
将检查一行是否包含该值789069
,然后按原样打印它并忽略其他行。
输出:
[{"cid":"PWER","data":[{"VALUE1":0}],"sid":"789069","units":"kWm","age":586667},
{"cid":"PWER_SUB","data":[{"VALUE5":0}],"sid":"789069","units":null,"age":4}