首页 > 解决方案 > JSONing 一个 Grepped 环境 (jq + grep + env)

问题描述

我只想从环境中获取 AWS_ 环境变量并将它们输出到 JSON 中。通常,做 env | grep AWS_ 向我显示正确的环境变量,而 jq -n env 会将整个环境显示为 JSON。

我试过了:

jq -n $(env | grep AWS_) 

jq -n $(env $(grep AWS_)) 

两者都没有成功。

标签: grepenvironment-variablesjq

解决方案


There are many options, but since (as you point out) jq has an env filter that produces the JSON for you, it would make sense to use it without having to invoke grep and then parse the output to convert it to JSON. For example:

jq -n 'env | with_entries(select(.key | test("AWS_")))'

You might want to change the test to use "^AWS_".


推荐阅读