我想从下面的JSON代码中获取一些数据:
我可以使用下面的命令得到输出,但现在我想格式化它,使我的输出看起来像预期的输出。
- 命令:**
cat dump | jq -r '["name","IP","NAT","location","method"],
(.objects[] | [.name, ."ipv4-address", ."nat-settings"."ipv4-address", ."nat-settings"."install-on", ."nat-settings".method])
| @csv'
| sed -e 's/"//g'
- 使用@csv后,我得到以下输出:**
name,IP,NAT,location,method
H_103.109.135.25,103.109.135.25,1.1.1.1,All,static
H_103.109.135.250,103.109.135.250,,,
每当我使用@tsv时,我得到**"jq:错误:tsv不是有效格式"**
有谁能建议我如何实现以下输出:
- 预期产出:**
- 原始JSON代码:**
{
"from" : 1,
"to" : 2,
"total" : 2,
"objects" : [ {
"uid" : "73b7036d-e8ec-47b7-99b5-19ca89eb5fd0",
"name" : "H_103.109.135.25",
"type" : "host",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"ipv4-address" : "103.109.135.25",
"interfaces" : [ ],
"nat-settings" : {
"auto-rule" : true,
"ipv4-address" : "1.1.1.1",
"ipv6-address" : "",
"install-on" : "All",
"method" : "static"
},
"comments" : "",
"color" : "black",
"icon" : "Objects/host",
"tags" : [ ],
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674820459413,
"iso-8601" : "2023-01-27T17:24+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1674818326777,
"iso-8601" : "2023-01-27T16:48+0530"
},
"creator" : "admin"
},
"read-only" : false,
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "true"
}
}, {
"uid" : "7300c38a-a496-497a-b9e3-5701fa081393",
"name" : "H_103.109.135.250",
"type" : "host",
"domain" : {
"uid" : "41e821a0-3720-11e3-aa6e-0800200c9fde",
"name" : "SMC User",
"domain-type" : "domain"
},
"ipv4-address" : "103.109.135.250",
"interfaces" : [ ],
"nat-settings" : {
"auto-rule" : false
},
"comments" : "",
"color" : "black",
"icon" : "Objects/host",
"tags" : [ ],
"meta-info" : {
"lock" : "unlocked",
"validation-state" : "ok",
"last-modify-time" : {
"posix" : 1674818341888,
"iso-8601" : "2023-01-27T16:49+0530"
},
"last-modifier" : "admin",
"creation-time" : {
"posix" : 1674818341888,
"iso-8601" : "2023-01-27T16:49+0530"
},
"creator" : "admin"
},
"read-only" : false,
"available-actions" : {
"edit" : "true",
"delete" : "true",
"clone" : "true"
}
} ]
}
- 注:**
只使用jq将输出打印在表格中并不是强制性的。"awk"或"sed"也可以。
我已经从以下原始json数据中提取了所需的数据:
- 提取数据:**
{
"name": "H_103.109.135.25",
"IP": "103.109.135.25",
"NAT": "1.1.1.1",
"location": "All",
"method": "static"
},
{
"name": "H_103.109.135.250",
"IP": "103.109.135.250",
"NAT": "NA",
"location": "NA",
"method": "NA"
}
我现在只需要将此数据格式化为如下表或类似的格式:
| name | IP | NAT | location | method |
|-------------------|-----------------|---------|------------|----------|
| H_103.109.135.25 | 103.109.135.25 | 1.1.1.1 | All | static |
| H_103.109.135.250 | 103.109.135.250 | NA | NA | NA |
3条答案
按热度按时间czq61nw11#
jbtl可能会产生你想要的结果。例如,如果你在
output.jq
中有这样的结果:然后通过这个过滤器传递数据,并使用
-m
选项将其传输到jtbl
,如下所示:给出了这个
x8diyxa72#
miller对于打印输出非常方便。
--c2p
是--icsv --opprint
的快捷方式,它读取CSV输入并输出打印精美的表格形式。miller
put
动词的脚本很笨拙。参见https://miller.readthedocs.io/en/latest/
更实用的款式:
我建议在jq中去掉引号并添加"NA",然后将输出通过管道传输到
column
假设"nat-settings"对象"缺少" ipv4-address "等键。
raogr8fs3#
我建议使用jq的
@tsv
和非常标准的工具column
,例如: