ptr*_*cao 4 shell floating-point json
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
}
]
Run Code Online (Sandbox Code Playgroud)
以便
"AttributeID": 7344.0
Run Code Online (Sandbox Code Playgroud)
会成为
"AttributeID": 7344
Run Code Online (Sandbox Code Playgroud)
例如,等等。
Kus*_*nda 11
只需通过身份过滤器运行它,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.)
Run Code Online (Sandbox Code Playgroud)
如果有小数点不为零的数字,并且您也想删除这些数字,请使用
jq '(.. | select(type == "number" )) |= floor' file.json
Run Code Online (Sandbox Code Playgroud)
这会将floor
函数应用于数据中的所有数字,将它们向下舍入为最接近的整数。
还要调查是否有字符串末尾的点后面包含数字,并删除这些数字(和点):
jq '(.. | select(type == "string")) |= sub("\\.[0-9]+$"; "")' file.json
Run Code Online (Sandbox Code Playgroud)
受影响的条目仍然是字符串,不会转换为数字类型。