可以在bash脚本中发出什么样的shell命令,以便有效地从JSON文件中的数字中删除所有十进制位,如下所示:
[
{
"IMSKU": "1000001",
"AttributeID": 7332.0,
"Value": "McAfee Host Intrusion Prevention for Desktops safeguards your business against complex security threats that may otherwise be unintentionally introduced or allowed by desktops and laptops. Host Intrusion Prevention for Desktops is easy to deploy, configure, and manage.",
"Unit": null,
"StoredValue": null,
"StoredUnit": null,
"Name": "Marketing text",
"Format": "1",
"Position": "1",
"Group_Name": "Basic Specification",
"AGGroup_Position": 0.0,
"Product_Hierarchy": 15198001453.0
},
{
"IMSKU": "1000001",
"AttributeID": 7343.0,
"Value": "May 2013",
"Unit": null,
"StoredValue": null,
"StoredUnit": null,
"Name": "PI Date",
"Format": "1",
"Position": "1",
"Group_Name": "PI DATE",
"AGGroup_Position": 1.0,
"Product_Hierarchy": 15198001453.0
},
{
"IMSKU": "1000001",
"AttributeID": 7344.0,
"Value": "McAfee",
"Unit": null,
"StoredValue": "0.00",
"StoredUnit": null,
"Name": "Brand Name",
"Format": "3",
"Position": "1",
"Group_Name": "PRODUCT",
"AGGroup_Position": 2.0,
"Product_Hierarchy": 15198001453.0
}
]所以
"AttributeID": 7344.0会变成
"AttributeID": 7344例如,等等。
发布于 2019-12-22 17:38:24
只要使用jq在标识筛选器中运行它,就可以将具有.0小数点的数字重新格式化为整数:
$ jq . file.json
[
{
"IMSKU": "1000001",
"AttributeID": 7332,
"Value": "McAfee Host Intrusion Prevention for Desktops safeguards your business against complex security threats that may otherwise be unintentionally introduced or allowed by desktops and laptops. Host Intrusion Prevention for Desktops is easy to deploy, configure, and manage.",
"Unit": null,
"StoredValue": null,
"StoredUnit": null,
"Name": "Marketing text",
"Format": "1",
"Position": "1",
"Group_Name": "Basic Specification",
"AGGroup_Position": 0,
"Product_Hierarchy": 15198001453
},
(etc.)如果有小数的小数不是零,而且你也想删除这些,请使用
jq '(.. | select(type == "number" )) |= floor' file.json这将对数据中的所有数字应用floor函数,将其舍入到最接近的整数。
还要研究是否有字符串包含在点后面的数字,并移除这些数字(和点):
jq '(.. | select(type == "string")) |= sub("\\.[0-9]+$"; "")' file.json受影响的条目仍然是字符串,不会转换为数字类型。
https://unix.stackexchange.com/questions/558510
复制相似问题