-1

I'm new to shell scripting and started baby steps in it.

I've written shell script recently to call rest API and I was able to execute it without any issues.

I've stored the output in a variable like below

{
  "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"
}

from the above output, I'm trying to find the "state" and its value. But I couldn't make that happen. kindly anyone helps me.

echo $result | grep -o 'state*'

with above command, I was able to print state. but I'm expecting both state and its value.

Appreciate your help. Thanks in Advance.

oguz ismail
  • 1
  • 16
  • 47
  • 69

2 Answers2

1

I tried storing your json in a file called 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"
}

Then use jq on top of this:

jq -r '.state' n2.json 
Completed
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
  • Yes, `jq` is a cleaner way to deal with JSON objects in bash. – Mayank Porwal Oct 18 '18 at 11:30
  • @SivaSankaraRaoBacha I used file, because it is quite cleaner to paste it here than pasting a variable. Otherwise the answer was simple, `echo $var | jq -r '.state' ` That was just for your understanding. – Mayank Porwal Oct 18 '18 at 11:32
0

You are looking for 'state', 'statee', 'stateee', 'stateeee', etc.

The wildcard applies to the preceeding character.

Try this:

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

It looks for everything up to, but excluding, the next comma.

Jack
  • 5,801
  • 1
  • 15
  • 20