2

I have a curl command

curl -u user:password -X POST -k http://artifactory:8081/artifactory/api/search/aql -d "items.find({\"type\" : \"folder\", \"repo\" : \"test-repo\", \"path\" : \""App_7.1.2"\", \"modified\" : {\"\$lt\" : \"2017-05-18\"}  })"

The output, that I get looks like this:

{
"results" : [ {
  "repo" : "test-repo",
  "path" : "App_7.1.2",
  "name" : "66",
  "type" : "folder",
  "size" : 0,
  "created" : "2016-05-26T09:40:03.332+03:00",
  "created_by" : "user",
  "modified" : "2016-05-26T09:40:03.332+03:00",
  "modified_by" : "user",
  "updated" : "2016-05-26T09:40:03.332+03:00"
},{
  "repo" : "test-repo",
  "path" : "App_7.1.2",
  "name" : "67",
  "type" : "folder",
  "size" : 0,
  "created" : "2016-05-31T19:19:35.040+03:00",
  "created_by" : "user",
  "modified" : "2016-05-31T19:19:35.040+03:00",
  "modified_by" : "user",
  "updated" : "2016-05-31T19:19:35.040+03:00"
} ]

I add a grep command to the command above and it starts looking like

curl -u user:password -X POST -k http://artifactory:8081/artifactory/api/search/aql -d "items.find({\"type\" : \"folder\", \"repo\" : \"test-repo\", \"path\" : \""App_7.1.2"\", \"modified\" : {\"\$lt\" : \"2017-05-18\"}  })" | grep -oP '\"name\" : \K.*' |tr -d '",'

and I get an output

66
67

So I can make an array from these strings by command:

($(curl -u user:password -X POST -k http://artifactory:8081/artifactory/api/search/aql -d "items.find({\"type\" : \"folder\", \"repo\" : \"test-repo\", \"path\" : \""App_7.1.2"\", \"modified\" : {\"\$lt\" : \"2017-05-18\"}  })" | grep -oP '\"name\" : \K.*' |tr -d '",'))

But what I actually need - is to get an array of Strings like path/name.

So it should be in my case

App_7.1.2/66
App_7.1.2/67

Could you tell me, how can I do it? I need to join different strings from output by using grep, is it possible? Thanks in advance

1 Answer 1

5

When working with JSON in Bash I suggest you pipe through jq. I think grep is the wrong tool for this job. This command gives you what you want:

curl https://example.com/ | jq --raw-output '.results | .[] | .path + "/" + .name'

The jq filters means:

  • .results - Read the "results" property of your JSON object
  • .[] - Read each element in the array, one by one
  • .path + "/" + .name - Concatenate the "path" property, the "/" string and the "name" property of each object

Then we add the --raw-output flag to output the result as lines instead of JSON.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.