JQ-如何定义过滤器以从输出数组中删除括号,引号和逗号 [英] JQ - How to define filter to remove brackets, quotes and commas from output array

查看:122
本文介绍了JQ-如何定义过滤器以从输出数组中删除括号,引号和逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将输出数组转换为不带方括号,引号和逗号的行,以便可以将其用于创建git克隆.

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

这是我的原始查询

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)]"  

返回格式为

[
   "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

要能够使用此命令,jq命令的输出必须像这样

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

第一部分是此链接

我需要对JQ过滤器进行哪些更改才能执行此操作?实际上,我需要从BitBucket项目中克隆40多个存储库,并且我想创建一个简单的脚本,而不必先获取列表.

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.

推荐答案

您只需将以下jq过滤器添加到生成两个字符串数组的过滤器上即可:

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

.[]

或更经济的是,只需从jq查询中删除外部方括号即可.

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

关键是-r选项仅在JSON字符串(不是复合实体中的字符串)的输出上去除双引号.

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

这篇关于JQ-如何定义过滤器以从输出数组中删除括号,引号和逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆