【问题标题】:How to join two values to print in one line using jquery如何使用jquery将两个值连接在一行中打印
【发布时间】:2022-08-18 01:18:48
【问题描述】:

我有这个使用打印出来的列表gh api命令:

    [
  {
    \"login\": \"anthonyd-cd\",
    \"id\": 35614549,
    \"node_id\": \"MDQ6VXNlcjM1NjE0NTQ5\",
    \"avatar_url\": \"https://avatars.githubusercontent.com/u/35614549?v=4\",
    \"gravatar_id\": \"\",
    \"url\": \"https://api.github.com/users/anthonyd-cd\",
    \"html_url\": \"https://github.com/anthonyd-cd\",
    \"followers_url\": \"https://api.github.com/users/anthonyd-cd/followers\",
    \"following_url\": \"https://api.github.com/users/anthonyd-cd/following{/other_user}\",
    \"gists_url\": \"https://api.github.com/users/anthonyd-cd/gists{/gist_id}\",
    \"starred_url\": \"https://api.github.com/users/anthonyd-cd/starred{/owner}{/repo}\",
    \"subscriptions_url\": \"https://api.github.com/users/anthonyd-cd/subscriptions\",
    \"organizations_url\": \"https://api.github.com/users/anthonyd-cd/orgs\",
    \"repos_url\": \"https://api.github.com/users/anthonyd-cd/repos\",
    \"events_url\": \"https://api.github.com/users/anthonyd-cd/events{/privacy}\",
    \"received_events_url\": \"https://api.github.com/users/anthonyd-cd/received_events\",
    \"type\": \"User\",
    \"site_admin\": false,
    \"contributions\": 5
  },
  {
    \"login\": \"elJoeyJojo\",
    \"id\": 17091968,
    \"node_id\": \"MDQ6VXNlcjE3MDkxOTY4\",
    \"avatar_url\": \"https://avatars.githubusercontent.com/u/17091968?v=4\",
    \"gravatar_id\": \"\",
    \"url\": \"https://api.github.com/users/elJoeyJojo\",
    \"html_url\": \"https://github.com/elJoeyJojo\",
    \"followers_url\": \"https://api.github.com/users/elJoeyJojo/followers\",
    \"following_url\": \"https://api.github.com/users/elJoeyJojo/following{/other_user}\",
    \"gists_url\": \"https://api.github.com/users/elJoeyJojo/gists{/gist_id}\",
    \"starred_url\": \"https://api.github.com/users/elJoeyJojo/starred{/owner}{/repo}\",
    \"subscriptions_url\": \"https://api.github.com/users/elJoeyJojo/subscriptions\",
    \"organizations_url\": \"https://api.github.com/users/elJoeyJojo/orgs\",
    \"repos_url\": \"https://api.github.com/users/elJoeyJojo/repos\",
    \"events_url\": \"https://api.github.com/users/elJoeyJojo/events{/privacy}\",
    \"received_events_url\": \"https://api.github.com/users/elJoeyJojo/received_events\",
    \"type\": \"User\",
    \"site_admin\": false,
    \"contributions\": 2
  }
]

我正在尝试使用jq只打印出登录贡献字段,但像这样进入一行:

gh api repos/eljoeyjojo/diner-app/contributors | jq -r \'.[].login, .[].contributions\'

预期结果

anthony-cd 5
eljoeyjojo 2

实际结果

anthony-cd
eljoeyjojo
5
2

知道如何打印/合并这两个字段吗?在这种情况下我是否必须使用 for 循环?

谢谢

    标签: github jq


    【解决方案1】:

    您正在使用迭代器 .[] 两次,因此每个字段都是从不同的迭代中提取的。将迭代器拉到前面并从迭代步骤的上下文中提取。然后你可以使用各种方法来组合它们,一种是字符串插值"\(...)"

    jq -r '.[] | "\(.login) \(.contributions)"'
    
    anthonyd-cd 5
    elJoeyJojo 2
    

    Demo

    【讨论】:

      【解决方案2】:

      对于您显示的示例,请尝试以下操作,使用-r 选项和jq 获取原始输出形式。

      jq -r '.[] | "\(.login) \(.contributions)"' Input-file
      

      【讨论】:

        猜你喜欢
        • 2022-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        相关资源
        最近更新 更多