gpt4 book ai didi

linux - 如何使用 grep 或 awk 查找字符串及其值

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:32 26 4
gpt4 key购买 nike

我是 shell 脚本新手,并且刚刚开始接触它。

我最近编写了 shell 脚本来调用 REST API,并且能够毫无问题地执行它。

我已将输出存储在如下变量中

{
"id": 3184136,
"name": "XXX TEST API",
"url": "http://xxxxxxxxxxx/_apis/test/Runs/3184136",
"isAutomated": true,
"owner": {
"displayName": "XXXX",
"url": "http://xxxxxxxxxxx/_apis/Identities/dbf722a9-73b0-46d6-a2bd-9835c1f0c221",
"_links": {
"avatar": {
"href": "http://xxxxxxxxxxx/_api/_common/identityImage?id=dbf722a9-73b0-46d6-a2bd-9835c1f0c221"
}
},
"id": "dbf722a9-73b0-46d6-a2bd-9835c1f0c221",
"uniqueName": "xxxxxxxxxxx\\ServiceLaunchpadDev",
"imageUrl": "http://xxxxxxxxxxx/_api/_common/identityImage?id=dbf722a9-73b0-46d6-a2bd-9835c1f0c221"
},
"project": {
"id": "6d5e21e7-c75e-464a-9708-90fbff086902",
"name": "eDellPrograms"
},
"startedDate": "2018-10-11T06:36:50.627Z",
"completedDate": "2018-10-11T07:04:45.153Z",
"state": "Completed",
"plan": {
"id": "5299555",
"name": "Smoke Dashboard Peso - DIT",
"url": "http://xxxxxxxxxxx/_apis/test/Plans/5299555"
},
"postProcessState": "Complete",
"totalTests": 5,
"incompleteTests": 0,
"notApplicableTests": 0,
"passedTests": 0,
"unanalyzedTests": 5,
"createdDate": "2018-10-11T06:36:50.533Z",
"lastUpdatedDate": "2018-10-11T07:04:45.153Z",
"lastUpdatedBy": {
"displayName": "xxxxxxxxxxx",
"url": "http://xxxxxxxxxxx/_apis/Identities/8de2a654-063b-48bd-8101-87e4ec2f05e3",
"_links": {
"avatar": {
"href": "http://xxxxxxxxxxx/_api/_common/identityImage?id=8de2a654-063b-48bd-8101-87e4ec2f05e3"
}
},
"id": "8de2a654-063b-48bd-8101-87e4ec2f05e3",
"uniqueName": "xxxxxxxxxxx\\xxxxxxxxxxx",
"imageUrl": "http://xxxxxxxxxxx/_api/_common/identityImage?id=8de2a654-063b-48bd-8101-87e4ec2f05e3"
},
"controller": "xxxxxxxxxxx",
"revision": 5,
"comment": "Build Definition : xxxxxxxxxxx \nBuild Version : xxxxxxxxxxx_20180925.1\nConfiguration : DIT\nBatch type : Suite\nTest type : Parallel\nTest Controller Name : xxxxxxxxxxx\nPreferred Agents : ADPTAW10A618|ADPTAW10A619|ADPTAW10A621 \nRequested by : xxxxxxxxxxx\nEmail Request : Y\nEmail To : xxxxxxxxxxx\nEmailCc : xxxxxxxxxxx\nEnvironment : DIT\nTest Setting : DIT\nContinue On Failure : false\nDNS Setting : false",
"dropLocation": "\\\\xxxxxxxxxxx\\DropFolder\\xxxxxxxxxxx_20180925.1",
"runStatistics": [
{
"state": "Completed",
"outcome": "Failed",
"count": 5
}
],
"webAccessUrl": "http://xxxxxxxxxxx/_TestManagement/Runs#runId=3184136&_a=runCharts"
}

从上面的输出中,我试图找到“状态”及其值。但我没能做到这一点。请任何人帮助我。

echo $result | grep -o 'state*'

使用上面的命令,我能够打印状态。但我期待状态及其值。

感谢您的帮助。提前致谢。

最佳答案

我尝试将您的 json 存储在名为 n2.json 的文件中。

cat n2.json
{
"id":3184136,
"name":"XXX TEST API",
"url":"http://xxxxxxxxxxx/_apis/test/Runs/3184136",
"isAutomated":true,
"owner":{
"displayName":"XXXX",
"url":"http://xxxxxxxxxxx/_apis/Identities/dbf722a9-73b0-46d6-a2bd-9835c1f0c221",
"_links":{
"avatar":{
"href":"http://xxxxxxxxxxx/_api/_common/identityImage?id=dbf722a9-73b0-46d6-a2bd-9835c1f0c221"
}
},
"id":"dbf722a9-73b0-46d6-a2bd-9835c1f0c221",
"uniqueName":"xxxxxxxxxxx\\ServiceLaunchpadDev",
"imageUrl":"http://xxxxxxxxxxx/_api/_common/identityImage?id=dbf722a9-73b0-46d6-a2bd-9835c1f0c221"
},
"project":{
"id":"6d5e21e7-c75e-464a-9708-90fbff086902",
"name":"eDellPrograms"
},
"startedDate":"2018-10-11T06:36:50.627Z",
"completedDate":"2018-10-11T07:04:45.153Z",
"state":"Completed",
"plan":{
"id":"5299555",
"name":"Smoke Dashboard Peso - DIT",
"url":"http://xxxxxxxxxxx/_apis/test/Plans/5299555"
},
"postProcessState":"Complete",
"totalTests":5,
"incompleteTests":0,
"notApplicableTests":0,
"passedTests":0,
"unanalyzedTests":5,
"createdDate":"2018-10-11T06:36:50.533Z",
"lastUpdatedDate":"2018-10-11T07:04:45.153Z",
"lastUpdatedBy":{
"displayName":"xxxxxxxxxxx",
"url":"http://xxxxxxxxxxx/_apis/Identities/8de2a654-063b-48bd-8101-87e4ec2f05e3",
"_links":{
"avatar":{
"href":"http://xxxxxxxxxxx/_api/_common/identityImage?id=8de2a654-063b-48bd-8101-87e4ec2f05e3"
}
},
"id":"8de2a654-063b-48bd-8101-87e4ec2f05e3",
"uniqueName":"xxxxxxxxxxx\\xxxxxxxxxxx",
"imageUrl":"http://xxxxxxxxxxx/_api/_common/identityImage?id=8de2a654-063b-48bd-8101-87e4ec2f05e3"
},
"controller":"xxxxxxxxxxx",
"revision":5,
"comment":"Build Definition : xxxxxxxxxxx \nBuild Version : xxxxxxxxxxx_20180925.1\nConfiguration : DIT\nBatch type : Suite\nTest type : Parallel\nTest Controller Name : xxxxxxxxxxx\nPreferred Agents : ADPTAW10A618|ADPTAW10A619|ADPTAW10A621 \nRequested by : xxxxxxxxxxx\nEmail Request : Y\nEmail To : xxxxxxxxxxx\nEmailCc : xxxxxxxxxxx\nEnvironment : DIT\nTest Setting : DIT\nContinue On Failure : false\nDNS Setting : false",
"dropLocation":"\\\\xxxxxxxxxxx\\DropFolder\\xxxxxxxxxxx_20180925.1",
"runStatistics":[
{
"state":"Completed",
"outcome":"Failed",
"count":5
}
],
"webAccessUrl":"http://xxxxxxxxxxx/_TestManagement/Runs#runId=3184136&_a=runCharts"
}

然后在此之上使用jq:

jq -r '.state' n2.json 
Completed

关于linux - 如何使用 grep 或 awk 查找字符串及其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52871849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com