JQ - How to define filter to remove brackets, quotes and commas from output array

╄→尐↘猪︶ㄣ 提交于 2020-06-27 18:41:31

问题


I need to convert an output array to lines without brackets, quotes and commas, so that it can be used to create git clones.

This is my original query

curl -s http://bitbucketlocalserver:7990/rest/api/1.0/projects/PROJECT_NAME/repos?per_page=20 ^
-u user:pass | H:\Downloads\Win64\jq-win64.exe -r "[.values[] | ((.links.clone[] | select(.name==\"http\") | .href)  + \" \" +  .name)]"  

which returns an output of the format

[
   "http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository1",
   "http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository2"
]

I want to use the output as input to another command like below

 curl -s http://bitbucketlocalserver:7990/rest/api/1.0/projects/PROJECT_NAME/repos?per_page=20 ^
-u user:pass | H:\Downloads\Win64\jq-win64.exe -r "[.values[] | ((.links.clone[] | select(.name==\"http\") | .href)  + \" \" +  .name)]"   | ^
 H:\Utilities\Git\usr\bin\xargs.exe -n 2 git clone -b release-dev

To be able to use this command, the output of the jq command needs to be like this

http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository1
http://bitbucketlocalserver:7990/scm/PROJECT_NAME/gitrepo1.git GitRepository2

The first part is part of this link

What changes do I need to make to the JQ filter so that I can perform this? In reality I need to clone more than 40 repositories from the BitBucket project and I would like to create a simple script where I do not have to get the list first.


回答1:


You could simply tack on the following jq filter to the filter that produces the array of two strings:

.[]

Or more economically, simply remove the outer square brackets from the jq query.

The point is that the -r option only strips the double-quotation marks on outputs that are JSON strings (not strings within compound entities).



来源:https://stackoverflow.com/questions/58566524/jq-how-to-define-filter-to-remove-brackets-quotes-and-commas-from-output-arra

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!