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

时间:2018-10-18 10:18:34

标签: linux bash shell

我是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*'

使用上述命令,我能够打印状态。但我期望状态和它的价值都可以。

感谢您的帮助。预先感谢。

2 个答案:

答案 0 :(得分:1)

我尝试将您的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

答案 1 :(得分:0)

您要查找“ state”,“ statee”,“ stateee”,“ stateeee”等。

通配符适用于前面的字符。

尝试一下:

echo $result | grep -o '"state":[^,]*'

它将查找直到(但不包括)下一个逗号的所有内容。