feat: add jq.md
(#27)
This commit is contained in:
parent
0bfe15e4dc
commit
f28c93b918
13
README.md
13
README.md
@ -42,7 +42,7 @@ Quick Reference
|
||||
[MySQL](./docs/mysql.md)<!--rehype:style=background: rgb(103 61 156/var(\-\-bg\-opacity));&class=tag&data-lang=SQL-->
|
||||
[MATLAB](./docs/matlab.md)<!--rehype:style=background: rgb(0 118 168/var(\-\-bg\-opacity));&class=contributing-->
|
||||
[Python](./docs/python.md)<!--rehype:style=background: rgb(240 81 57/var(\-\-bg\-opacity));-->
|
||||
[PostgreSQL](./docs/postgres.md)<!--rehype:style=background: rgb(43 109 163/var(\-\-bg\-opacity));-->
|
||||
[PostgreSQL](./docs/postgres.md)<!--rehype:style=background: rgb(43 109 163/var(\-\-bg\-opacity));&class=tag&data-lang=SQL-->
|
||||
[Rust](./docs/rust.md)<!--rehype:style=background: rgb(71 71 71/var(\-\-bg\-opacity));-->
|
||||
[Swift](./docs/swift.md)<!--rehype:style=background: rgb(240 81 57/var(\-\-bg\-opacity));-->
|
||||
[SwiftUI](./docs/swiftui.md)<!--rehype:style=background: rgb(10 127 247/var(\-\-bg\-opacity));&class=tag&data-lang=swift-->
|
||||
@ -86,16 +86,16 @@ Quick Reference
|
||||
[Gmail](./docs/gmail.md)<!--rehype:style=background: rgb(234 67 54/var(\-\-bg\-opacity));-->
|
||||
[nginx](./docs/nginx.md)<!--rehype:style=background: rgb(0 153 0/var(\-\-bg\-opacity));-->
|
||||
[Semver](./docs/semver.md)<!--rehype:style=background: rgb(106 111 141/var(\-\-bg\-opacity));-->
|
||||
[Sketch](./docs/sketch.md)<!--rehype:style=background: rgb(223 148 0/var(\-\-bg\-opacity));-->
|
||||
[Sketch](./docs/sketch.md)<!--rehype:style=background: rgb(223 148 0/var(\-\-bg\-opacity));&class=tag&data-lang=macOS-->
|
||||
[Sublime Text](./docs/sublime-text.md)<!--rehype:style=background: rgb(223 148 0/var(\-\-bg\-opacity));-->
|
||||
[VSCode](./docs/vscode.md)<!--rehype:style=background: rgb(91 163 230/var(\-\-bg\-opacity));-->
|
||||
[Vim](./docs/vim.md)<!--rehype:style=background: rgb(9 150 8/var(\-\-bg\-opacity));-->
|
||||
[WebStorm](./docs/webstorm.md)<!--rehype:style=background: rgb(32 148 220/var(\-\-bg\-opacity));-->
|
||||
[XPath](./docs/xpath.md)<!--rehype:style=background: rgb(91 163 230/var(\-\-bg\-opacity));-->
|
||||
[Xcode](./docs/xcode.md)<!--rehype:style=background: rgb(24 151 233/var(\-\-bg\-opacity));-->
|
||||
[Xcode](./docs/xcode.md)<!--rehype:style=background: rgb(24 151 233/var(\-\-bg\-opacity));&class=tag&data-lang=macOS-->
|
||||
<!--rehype:class=home-card-->
|
||||
|
||||
## Linux 命令
|
||||
## 命令
|
||||
|
||||
[Ansible](./docs/ansible.md)<!--rehype:style=background: rgb(238 0 0/var(\-\-bg\-opacity));&class=contributing tag&data-lang=RedHad&data-info=👆看看还缺点儿什么?-->
|
||||
[Awk](./docs/awk.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
@ -110,8 +110,9 @@ Quick Reference
|
||||
[Find](./docs/find.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
[FFmpeg](./docs/ffmpeg.md)<!--rehype:style=background: rgb(0 193 9/var(\-\-bg\-opacity));&class=contributing-->
|
||||
[Htop](./docs/htop.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
[Home Brew](./docs/homebrew.md)<!--rehype:style=background: rgb(252 185 87/var(\-\-bg\-opacity));-->
|
||||
[Home Brew](./docs/homebrew.md)<!--rehype:style=background: rgb(252 185 87/var(\-\-bg\-opacity));&class=tag&data-lang=macOS-->
|
||||
[Netstat](./docs/netstat.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
[jq](./docs/jq.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));&class=tag&data-lang=JSON-->
|
||||
[Lsof](./docs/lsof.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
[Netcat](./docs/netcat.md)<!--rehype:style=background: rgb(4 92 135/var(\-\-bg\-opacity));-->
|
||||
[Sed](./docs/sed.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
@ -122,7 +123,7 @@ Quick Reference
|
||||
|
||||
## 其它
|
||||
|
||||
[Quick Reference](./docs/quickreference.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
[Quick Reference](./docs/quickreference.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));&class=tag&data-lang=排版说明-->
|
||||
[Github Actions](./docs/github-actions.md)<!--rehype:style=background: rgb(121 184 255/var(\-\-bg\-opacity));-->
|
||||
[Colors Named](./docs/colors-named.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));&class=tag&data-lang=CSS-->
|
||||
[HTTP 状态码](./docs/http-status-code.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));-->
|
||||
|
236
docs/jq.md
Normal file
236
docs/jq.md
Normal file
@ -0,0 +1,236 @@
|
||||
jq 备忘清单
|
||||
===
|
||||
|
||||
这个快速参考备忘单提供了使用 [jq](https://stedolan.github.io/jq/) 命令的各种方法。
|
||||
|
||||
入门
|
||||
----
|
||||
|
||||
### 介绍
|
||||
|
||||
jq 就像用于 JSON 数据的 [sed](./sed.md) - 您可以使用它来切片、过滤、映射和转换结构化数据
|
||||
|
||||
- [jq 官网](https://stedolan.github.io/jq/) _(stedolan.github.io)_
|
||||
- [jq 命令使用](https://jaywcjlove.github.io/linux-command/c/jq.html) _(jaywcjlove.github.io)_
|
||||
|
||||
安装
|
||||
|
||||
```bash
|
||||
$ sudo apt-get install jq # Debian& Ubuntu
|
||||
$ sudo dnf install jq # Fedora
|
||||
$ sudo zypper install jq # openSUSE
|
||||
$ sudo pacman -S jq # Arch
|
||||
$ brew install jq # macOS & Homebrew
|
||||
$ port install jq # macOS & MacPorts
|
||||
```
|
||||
|
||||
语法
|
||||
|
||||
```bash
|
||||
$ jq [options] <jq filter> [file...]
|
||||
$ jq [options] --args <jq filter> [strings...]
|
||||
$ jq [options] --jsonargs <jq filter> [JSON_TEXTS...]
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
### 选项
|
||||
<!--rehype:wrap-class=col-span-2-->
|
||||
|
||||
:- | -
|
||||
:- | -
|
||||
`-c` | 紧凑而不是漂亮的输出
|
||||
`-n` | 使用`null`作为单个输入值
|
||||
`-e` | 根据输出设置退出状态代码
|
||||
`-s` | 将所有输入读取(吸取)到数组中;应用过滤器
|
||||
`-r` | 输出原始字符串,而不是JSON文本
|
||||
`-R` | 读取原始字符串,而不是JSON文本
|
||||
`-C` | 为 JSON 着色
|
||||
`-M` | 单色(不要为JSON着色)
|
||||
`-S` | 在输出上排序对象的键
|
||||
`--tab` | 使用制表符进行缩进
|
||||
`--arg a v` | 将变量 `$a` 设置为 value `<v>`
|
||||
`--argjson a v` | 将变量 `$a` 设置为 JSON value `<v>`
|
||||
`--slurpfile a f` | 将变量 `$a` 设置为从`<f>`读取的JSON文本数组
|
||||
`--rawfile a f` | 将变量 `$a` 设置为包含`<f>`内容的字符串
|
||||
`--args` | 其余参数是字符串参数,而不是文件
|
||||
`--jsonargs` | 其余的参数是JSON参数,而不是文件
|
||||
`--` | 终止参数处理
|
||||
|
||||
学习示例
|
||||
---
|
||||
|
||||
### 获取一个键的值
|
||||
|
||||
```bash
|
||||
$ echo '{"foo": 42, "bar": "less interesting data"}' | jq '.foo'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
42
|
||||
```
|
||||
|
||||
### 数组运算
|
||||
|
||||
```bash
|
||||
$ echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' | jq '.[1]'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
{
|
||||
"name": "XML",
|
||||
"good": false
|
||||
}
|
||||
```
|
||||
|
||||
### 构造一个数组/对象
|
||||
|
||||
```bash
|
||||
$ echo '{"user":"stedolan","titles":["JQ Primer", "More JQ"]}' | jq '{user, title: .titles[]}'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
{
|
||||
"user": "stedolan",
|
||||
"title": "JQ Primer"
|
||||
}
|
||||
{
|
||||
"user": "stedolan",
|
||||
"title": "More JQ"
|
||||
}
|
||||
```
|
||||
|
||||
### 计算一个值的长度
|
||||
|
||||
```bash
|
||||
$ echo '[[1,2], "string", {"a":2}, null]' | jq '.[] | length'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
2
|
||||
6
|
||||
1
|
||||
0
|
||||
```
|
||||
|
||||
### 取出数组中的键
|
||||
|
||||
```bash
|
||||
$ echo '{"abc": 1, "abcd": 2, "Foo": 3}' | jq 'keys'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
[
|
||||
"Foo",
|
||||
"abc",
|
||||
"abcd"
|
||||
]
|
||||
```
|
||||
|
||||
### 使用多个过滤器
|
||||
|
||||
```bash
|
||||
$ echo '{ "foo": 42, "bar": "something else", "baz": true}' | jq '.foo, .bar'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
42
|
||||
"something else"
|
||||
```
|
||||
|
||||
### 管道传递给下一个过滤器
|
||||
|
||||
```bash
|
||||
$ echo '[{"name":"JSON", "good":true}, {"name":"XML", "good":false}]' | jq '.[] | .name'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
"JSON"
|
||||
"XML"
|
||||
```
|
||||
|
||||
### 条件语句判断
|
||||
|
||||
```bash
|
||||
$ echo '[1,5,3,0,7]' | jq 'map(select(. >= 2))'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
[
|
||||
5,
|
||||
3,
|
||||
7
|
||||
]
|
||||
```
|
||||
|
||||
### 每个输入调用过滤器
|
||||
|
||||
```bash
|
||||
$ echo '[1,2,3]' | jq 'map(.+1)'
|
||||
```
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
[
|
||||
2,
|
||||
3,
|
||||
4
|
||||
]
|
||||
```
|
||||
|
||||
### 条件判断
|
||||
|
||||
```bash
|
||||
$ echo '2' | jq 'if . == 0 then "zero" elif . == 1 then "one" else "many" end'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
"many"
|
||||
```
|
||||
|
||||
### 字符串插入值并进行运算
|
||||
|
||||
```bash
|
||||
$ echo '42' | jq '"The input was \(.), which is one less than \(.+1)"'
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
输出结果
|
||||
|
||||
```bash
|
||||
"The input was 42, which is one less than 43"
|
||||
```
|
||||
<!--rehype:className=wrap-text -->
|
||||
|
||||
另见
|
||||
----
|
||||
|
||||
- [jq 官网](https://stedolan.github.io/jq/) _(stedolan.github.io)_
|
||||
- [jq 命令使用](https://jaywcjlove.github.io/linux-command/c/jq.html) _(jaywcjlove.github.io)_
|
Loading…
x
Reference in New Issue
Block a user