我有以下grep声明:
kernelHash=$(grep -m 1 -o '"hashvalue": [^, }]*' $SecureBootJson | sed 's/^.*: //')
每晚都会生成一个新文件(请参阅下面的示例),并设置所有新的哈希值。有一个独特的文字,其中“hashvalue”(“asomething2”)使生活更轻松。 Grep只需要寻找“asomething2”。但是现在我需要在something2块中分配给“hashvalue”的hashvalue。我不得不将“asomething2”改为“hashvalue”。问题是该文件有15个“hashvalue”文字,我只需要它来查看第二个文件。我怎么能告诉grep只从文件中挑出第二个“hashvalue”?
以下是该文件的示例:
{
"files":[
{
"something1":{
"sources":{
"asomething1":"3842d9bd57cd5b0b6f4388b55230a188e093b435d89093e5f90099d174167d71",
"bsomething1":"229448f5d12e0e5c683136aef83243f8eec142873b9ebc48f3852a867aafee9d"
},
"hashvalue":"3536b8ad291ad33b0b39b59e59bec1c98a01473cfc0507a0e926b79fac580b6c",
"type":"bootloader",
"mode":"2ND"
}
},
{
"something2":{
"sources":{
"asomething2":"6685c4a147ca318f2e870b8a616fd2ccb188868ab10cceb7c68d5a4bda33c16f"
},
"hashvalue":"cda2e741d6229f670963f52ea5b6427e1fcdb9bc66bdfe338efcb70ac5b3d61c",
"type":"image"
}
},
{
"something3":{
"sources":{
"asomething3":"4d9530a3047ef20d67b2d91380416925e9e8867f28db27e2a138e4c48d34b95b"
},
"hashvalue":"7f43440bb71de0c99b1f110f1fb73df36a63904d3e80d8c39b7fe34c99143fbe",
"type":"image"
}
},
{
"something4":{
"hashvalue":"613635f9749db37a133d81a7d70bcd8d2abc336576b30463561e9fc7ef02f31b",
"type":"passthru"
}
},
{
"something5":{
"hashvalue":"8cee68c3b2a4975b03f8fa2cab2bda7bd04646d43470503c0efdac7719ecc756",
"type":"passthru"
}
},
{
"something6":{
"hashvalue":"6a1b81d9a623df9a42e382008e531f67aff5cd64b292a403f01339e80edb14d6",
"type":"passthru"
}
},
{
"something7":{
"hashvalue":"200e162f014d6555820c5e69b80cba53ee19c4f289f2352833a0faf86b230031",
"type":"passthru"
}
},
{
"something8":{
"hashvalue":"4d9530a3047ef20d67b2d91380416925e9e8867f28db27e2a138e4c48d34b95b",
"type":"passthru"
}
},
{
"something9":{
"hashvalue":"07258ddb82082c71b81a760cd1c540ca35a52250cb8f94ca90b97514f0df7fff",
"type":"manifest"
}
},
{
"something10":{
"hashvalue":"74732b0224bdb1235670f0041d4cabb8010a3e2fcb55dea5ce003673b23f1e9a",
"type":"manifest"
}
},
{
"something11":{
"hashvalue":"d80dc4ada8e9b611803fbd4d28d37a621618c2c1b39b4d470a4e622b0dbf3f1d",
"type":"manifest"
}
},
{
"something12":{
"hashvalue":"84ff4132f25a7f6d468374afb9f1f4ba4711e67a3e5cce712d77368eecd45194",
"type":"manifest"
}
},
{
"something13":{
"hashvalue":"fdceda07f28179da26621ef9a72c2de0627af12afe7e6784590ec67d55a21820",
"type":"manifest"
}
},
{
"something14":{
"hashvalue":"5d77074465c752b9197bbf4b54e599173b9e49540a56784758862e502dea2519",
"type":"manifest"
}
},
{
"something15":{
"hashvalue":"a53a1a9129ca98b6d10f67a0a3e630ed4a1851ffedf96d91b79b41f61ea00d4b",
"type":"manifest"
}
}
}
我想要的输出是分配给kernelHash的something2块中“hashvalue”的哈希值:
kernelHash = cda2e741d6229f670963f52ea5b6427e1fcdb9bc66bdfe338efcb70ac5b3d61c
答案 0 :(得分:1)
grep 解决方案:
示例json文件input.json
:
{
"key1": "abc",
"hashvalue":"cd11111111111111111sdfsdfw1111111",
"key2": "def",
"secure.data": {
"hashvalue":"cda2e741d6229f670963f52ea5b6427e1fcdb9bc66bdfe338efcb70ac5b3d61c",
"key3": "ghi"
},
"key4":"jkl",
"hashvalue":"22222sfsdfsdfsdf33333333"
}
提取第二个hashvalue
并分配到变量kernelHash
:
kernelHash=$(grep -Pzo '("hashvalue":)"[^"]+",[\s\S]+?\1\K"[^"]+"' input.json)
echo "$kernelHash"
"cda2e741d6229f670963f52ea5b6427e1fcdb9bc66bdfe338efcb70ac5b3d61c"