linux jq 使用

1,安装jq

# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.3 LTS
Release:	18.04
Codename:	bionic
  • jq 安装
apt install jq -y
  • 取某个值
    .key, .foo.bar, .["key"]
# cat config.json | jq .mining
{
  "minerAddress": "t2quorlsyipjq5koh6pbtxctxd76lljwcrirj2oky",
  "autoSealIntervalSeconds": 120,
  "storagePrice": "0.000000001"
}

# cat config.json | jq .mining.minerAddress
"t2quorlsyipjq5koh6pbtxctxd76lljwcrirj2oky"

2,在一行输出

  • 使用参数-c

3,去掉字符串的引号

-r 以原始输出格式打印结果,而不是默认的带引号的字符串格式。

# lotus mpool pending | jq .Message.GasFeeCap | head
"101041"
"100944"
"101182"
# lotus mpool pending | jq -r '.Message.GasFeeCap' | head
100591
101804
101732

参考:

  1. jq 常用操作
  2. Shell:无比强大的shell之json解析工具jq , Linux命令行解析json, jq解析 json 实例