json - Curl 和 jq 替换值中的子字符串

标签 json string command-line edit jq

使用curl,我从github api获取json:

curl https://api.github.com/repos/angular/angular/pulls?label=comp%3A+zones&state=closed&page=1&per_page=5

结果:

[
  {
    "url": "https://api.github.com/repos/angular/angular/pulls/33703",
    "id": 338899730,
    "node_id": "MDExOlB1bGxSZXF1ZXN0MzM4ODk5NzMw",
    "html_url": "https://github.com/angular/angular/pull/33703",
    "diff_url": "https://github.com/angular/angular/pull/33703.diff",
    "patch_url": "https://github.com/angular/angular/pull/33703.patch",
    "issue_url": "https://api.github.com/repos/angular/angular/issues/33703",
    "number": 33703,
    "state": "closed",
    "locked": false,
    "title": "add AOT mode default to version 9 upgrade doc",

      [...]

与jq:

$ curl 'https://api.github.com/repos/angular/angular/pulls?label=comp%3A+zones&state=closed&page=1&per_page=2' | jq --raw-output '.[] | "\(.id) \(.title)"'

我可以很好地将其格式化为

338899730 add AOT mode default to version 9 upgrade doc
338868666 update jiali's info on collaborator page of angular.io

如何在输出之前从标题值中删除addupdate

最佳答案

使用sub,例如:

.[]
| "\(.id) \(.title|sub("^(add|update) *";""))"

关于json - Curl 和 jq 替换值中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58801982/

相关文章:

javascript - 表绑定(bind)以及使用 htmlspecialchars 时 - JSON.parse 错误

c++ - 字符串统计函数

c++ - 有没有办法确定 sprintf 将写入多少个字符?

Java Runtime.exec 在 Linux 中因空间而失败

命令行输入作为参数

mobile - 从命令行使用 Azure 移动服务进行测试

android - 如何使用 gson 在 Kotlin 中的 Json 数组中插入 Json 对象

javascript - 我有一个有效的 json 但我不断收到 "syntaxerror json.parse unexpected end of data at line 1"!

java - 反序列化 JSON 对象

javascript - 如何替换除第一次出现以外的所有匹配字符