gron 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

gron 命令安装:


-bash/zsh: gron command not not found

# Windows (WSL2)
sudo apt-get update sudo apt-get install gron

# Debian
apt-get install gron

# Ubuntu
apt-get install gron

# Kali Linux
apt-get install gron

# OS X
brew install gron

# Dockerfile
dockerfile.run/gron

gron 命令补充说明:


gron 使用 Go 语言编写的将 JSON 转换为离散的分配工具,以便更轻松地 grep 所需内容并查看其绝对 路径。gron 可以从本地文件、通过网络或直接从 STDIN 读取 JSON。

gron 简化了返回大量 JSON 但文档糟糕的 API 的探索。gron 也可以向后工作,使您能够将过滤后的数据转换回 JSON。

gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" | fgrep "commit.author"

json[0].commit.author = {};
json[0].commit.author.date = "2025-06-02T10:51:21Z";
json[0].commit.author.email = "mail@tomnomnom.com";
json[0].commit.author.name = "Tom Hudson";

gron 命令语法:


gron [OPTIONS] [file]
gron [OPTIONS] [URL]

gron 命令选项:


-c, --color:强制彩色输出(即使非终端环境)。
-i, --indent:指定缩进空格数(默认 2)。
-n, --no-sort:不排序输出结果。
-u, --ungron:将 gron 格式转回 JSON。
--json:等同于 --ungron,但更符合语义。
-v, --values: 仅输出值的部分(不包括路径)
-s, --stream: 将每行输入视为单独的 JSON 对象处理

gron 命令实例:


gron data.json 文件示例:

{
  "name": "Alice",
  "age": 30,
  "pets": [
    {"name": "Rex", "type": "dog"},
    {"name": "Whiskers", "type": "cat"}
  ],
  "address": {
    "city": "New York",
    "zip": "10001"
  }
}

gron 转换 JSON 为 gron 格式:

gron data.json

# Output
json = {};
json.name = "Alice";
json.age = 30;
json.pets = [];
json.pets[0] = {};
json.pets[0].name = "Rex";
json.pets[0].type = "dog";
json.pets[1] = {};
json.pets[1].name = "Whiskers";
json.pets[1].type = "cat";
json.address = {};
json.address.city = "New York";
json.address.zip = "10001";

gron 搜索特定的值:

gron data.json | grep "zip"

# Output
json.address.zip = "10001";

gron 恢复之前的 JSON 格式 --ungron

gron data.json | grep "pets" | gron --ungron

# Output
{
  "pets": [
    {
      "name": "Rex",
      "type": "dog"
    },
    {
      "name": "Whiskers",
      "type": "cat"
    }
  ]
}

gron 使用自定义变量名 -s

gron -s data data.json

# Output
data = {};
data.name = "Alice";

gron 流式处理大型文件 --stream

curl -s https://api.example.com/large-data | gron --stream

gron 指定输出格式 -j --json

gron data.json -j | grep "name"

# Output
$.name = "Alice";
$.pets[0].name = "Rex";
$.pets[1].name = "Whiskers";

gron 结合 awk 处理数据:

gron data.json | awk '/pets/ && /type/ {print $3}'

# Output
"dog"
"cat"

gron 使用 jq 风格路径:

gron -j data.json | grep 'pets.*name'

# Output
$.pets[0].name = "Rex";
$.pets[1].name = "Whiskers";

gron 处理多个文件:

gron file1.json file2.json | grep "error"

gron 修改并恢复数据:

gron data.json | sed 's/"New York"/"Boston"/' | gron --ungron

gron 处理压缩数据:

zcat large.json.gz | gron --stream | grep "error"

gron 提取所有键名 key

gron data.json | awk -F '.' '{print $2}' | sort | uniq

gron 调试 API 响应:

curl -s https://api.github.com/users/octocat | gron | grep "company"

gron 处理复杂配置文件:

gron config.json | grep "database.password"

gron 搜索嵌套值:

gron data.json | grep "pets.*cat"

gron 颜色高亮输出:

gron data.json | grep --color=auto "name"

gron 命令扩展阅读:


 

CommandNotFound ⚡️ 坑否 - 其他频道扩展阅读:




gron 命令评论

共收录到 539Linux 命令