Bash 使用grep在输出中构造字符串

Bash 使用grep在输出中构造字符串,bash,shell,grep,Bash,Shell,Grep,我有一个卷曲命令 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\"} })" 我得到的输出如下所示: { "

我有一个卷曲命令

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\"}  })"
我得到的输出如下所示:

{
"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"
} ]
我在上面的命令中添加了一个grep命令,它看起来像

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 '",'
我得到一个输出

66
67
因此,我可以通过命令从这些字符串生成数组:

($(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 '",'))
但我真正需要的是得到一个字符串数组,比如path/name

所以我的情况应该是这样

App_7.1.2/66
App_7.1.2/67

你能告诉我怎么做吗?我需要使用grep从输出连接不同的字符串,可以吗?提前感谢

在Bash中使用JSON时,我建议您通过管道。我认为grep是这个工作的错误工具。此命令提供您想要的:

curl https://example.com/ | jq --raw-output '.results | .[] | .path + "/" + .name'
jq过滤器意味着:

  • .results
    -读取JSON对象的“results”属性
  • []
    -逐个读取数组中的每个元素
  • .path+“/”+.name
    -连接每个对象的“path”属性、“/”字符串和“name”属性
然后我们添加
--raw output
标志,将结果输出为行,而不是JSON