在shell脚本中获取逗号分隔值

时间:2018-05-30 23:58:04

标签: linux shell sh

我有一个包含多行的文件,如下面的字符串。我需要提取id和obj的值。

{"Class":"ZONE","id":"DEV100.ZN301","name":"3109 E BTM","zgroup":"EAST FLOOR 3","prog":"","members":[{"obj":"DEV300.WC3"},{"obj":"DEV300.WC4"},{"obj":"DEV300.WC7"},{"obj":"DEV300.WC10"}]}

我使用以下命令获取输出:

[user@server ~]$ cat file.txt | grep "\"Class\":\"ZONE\"" | while IFS="," read -r a b c d e f ; do echo "$b$f";done | grep ZN301

输出:

"id":"DEV100.ZN301""members":[{"obj":"DEV300.WC3"},{"obj":"DEV300.WC4"},{"obj":"DEV300.WC7"},{"obj":"DEV300.WC10"}]}

我的目标是获得以下输出

DEV100.ZN301 : DEV300.WC3 , DEV300.WC4 , DEV300.WC7 , DEV300.WC10

请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:2)

<TextInput placeholderTextColor={'red'} />

...或者,依靠Python解释器:

jq -r 'select(.Class == "ZONE") | (.id + " : " + ([.members[] | .obj] | join(" , ")))'