底部是一些示例json数据,我想添加时间戳。即"time": "1544785866.176123088"
时间戳在每次迭代中都必须是唯一的,并且需要出现在“ bladesetName”上方的行上。不幸的是,到目前为止,我没有用。将时间打印到所需位置的文件后,我将为每个条目添加1ns。有更好的方法吗?
timestamp() {
date +%s.%N
}
awk "/bladesetName/{ print '"time" "$(timestamp)"' }1" volumes3.json
这会引发错误
awk: /bladesetName/{ print 'time 1544786158.644385726' }1
awk: ^ invalid char ''' in expression
。
{
"pasxml": {
"@version": "6.0.0",
"system": {
"name": "example1",
"IPV4": "0.0.0.0",
"alertLevel": "critical",
"state": "online"
},
"volumes": {
"volume": [
{
"@id": "1",
"name": "/",
"bladesetName": {
"@id": "1",
"#text": "Set-1"
},
"state": "Online",
"raid": "Object RAID6+",
"director": "Shelf-001,1",
"volservice": "0x0400000000000004(FM)",
"objectId": "I-xD0200000000000004-xG7ee84b0c-xU00004a75726a656e",
"recoveryPriority": "1",
"efsaMode": "retry",
"spaceUsedGB": "0",
"spaceAvailableGB": "693903.38",
"hardQuotaGB": "0.52",
"softQuotaGB": "0.52",
"userQuotaPolicy": {
"@inherit": "0",
"#text": "disabled"
},
"stats": null
},
{
"@id": "8",
"name": "/datacentre/archvol/pan101",
"bladesetName": {
"@id": "1",
"#text": "Set-1"
},
"alertLevel": "critical",
"state": "Online",
"raid": "Object RAID6+",
"director": "Shelf-008,1",
"volservice": "0x04000000000000ec(FM)",
"objectId": "I-xD02000000000000ec-xG5c7aef6f-xU00004a75726a656e",
"recoveryPriority": "50",
"efsaMode": "retry",
"spaceUsedGB": "117000.09",
"spaceAvailableGB": "693903.38",
"hardQuotaGB": "117000.00",
"softQuotaGB": "90000.00",
"userQuotaPolicy": {
"@inherit": "1",
"#text": "disabled"
},
"stats": null
},
答案 0 :(得分:0)
这是一种使用jq的方法:
def addnow:
. as $in
| to_entries
| (map(.key)|index("bladesetName")) as $i
| if $i
then .[0:$i]+[{key:"time",value:now}]+.[$i:] | from_entries
else $in
end ;
walk(if type=="object" then addnow else . end)
如果您确实希望将时间作为字符串,只需添加tostring
,就像在(now|tostring)
中一样
如果您的jq没有walk
,那么现在是升级的好时机;否则,只需复制并粘贴其def,就可以轻松在网上找到它,例如通过谷歌搜索:jq“ def walk”