dsq 和 jq 操作记录

  1. 通过 dsq join 文件条件查找

本地文件处理工作流整理,待归档到一个合适的目录

通过 dsq join 文件条件查找

安装工具

// 文件 a.json
[
    {"type": "tool", "id": "1"},
    {"type": "tool", "id": "2"},
    {"type": "tool", "id": "3"},
    {"type": "book", "id": "1"},
    {"type": "none", "id": "1"}
]
// 文件 b.json
[
    {"author": "nickchen", "type":"tool", "tag":[1,2,3]},
    {"author": "whoru", "type":"none"},
    {"author": "teacher", "type":"book"}
]

需求打印出 author、type、id ,且 id=1 的组合

  1. 因为 dsq 没法解析内嵌列表,所有 b.json 中的 tag 字段必须要被删除
  2. 需要 dsq join a.json 和 b.json,且判断 id = 1
  3. 打印结果确认是否符合要求
  4. 输出一个格式化的 json array

开始执行:

  1. 清理 b.json 中的 tag 字段
    • jq 的最外侧 [] 是列表构造器,会生成一份列表
    • jq 的 .[] 代表迭代器,迭代当前这个 json array
    • jq 的 | 是 pipe 管道,将数据流转到下一个程序
    • jq 的 del() 是内建函数,可以删除某个字段
➜  cat b.json | jq '[.[]|del(.tag)]' > c.json
or
➜  cat b.json | jq '[.[]|{author:.author,type:.type}]' > c.json
  1. dsq join 查询 & 打印结果
    • dsq 的 --pretty 可以输出表格结果
    • dsq 输入多个文件,可以按照进行 join 操作,表名即文件顺序
➜  dsq --pretty a.json c.json "select {0}.id, {1}.type, {1}.author from {0} join {1} on {0}.type = {1}.type where {0}.id = 1"
+----------+----+------+
|  author  | id | type |
+----------+----+------+
| nickchen |  1 | tool |
| teacher  |  1 | book |
| whoru    |  1 | none |
+----------+----+------+
  1. 输出一个格式化的 json array
➜  dsq a.json c.json "select {0}.id, {1}.type, {1}.author from {0} join {1} on {0}.type = {1}.type where {0}.id = 1" | jq .
[
  {
    "id": "1",
    "type": "tool",
    "author": "nickchen"
  },
  {
    "type": "book",
    "author": "teacher",
    "id": "1"
  },
  {
    "id": "1",
    "type": "none",
    "author": "whoru"
  }
]

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 nickchenyx@gmail.com

Title:dsq 和 jq 操作记录

Count:567

Author:nickChen

Created At:2022-03-11, 20:12:27

Updated At:2023-05-08, 23:27:10

Url:http://nickchenyx.github.io/2022/03/11/local-file-process/

Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.