[1]: https://kislyuk.github.io/yq/
[2]: https://github.com/TomWright/dasel
[3]: https://hclq.sh/
[1]: https://kislyuk.github.io/yq/
[2]: https://github.com/TomWright/dasel
[3]: https://hclq.sh/
https://github.com/tomnomnom/gron
I've been using `jq` for years and I'm always able to cobble together what I need, but I have yet to find it intuitive and I'm rarely able to arrive at a solution of any complexity without spending a lot of time reading its documentation. I wish I found it easier to use. :-(
But ChatGPT has genuinely solved my suffering writing jq, it does a pretty good job. It even almost replaces gron, if you feed it an exmaple json and ask for jq, it gives you something. It usually needs a little adjusting but it gets me 90% of the way there and saves me a bit of time.
I rarely use it for much else but its a jq winner :)
jq -r 'paths(scalars) as $p | getpath($p) | "\($p|join(".")) = \(.)"'
See elsewhere in this subthread for a full gron implementation in jq.[0] https://www.parkersoftware.com/blog/stop-using-simply-in-tec...
grep -A1 foo | grep -B1 bar
Will find a line with "foo" followed by a line with "bar" and emit both. Of course, it will also find a single line with both "foo" and "bar", so it's not perfect. This is a quick and dirty solution. Beyond that, break out sed and awk, or maybe the Practical Extraction and Report Language... it's really good at that stuff.The problem here is literally that someone hardcoded "IT'S ALWAYS LINEFEED" into an algorithm that could work equally well with any record separator character -- in fact probably with any record separator regex. I notice there's now `grep -z` which is one small step towards sanity... but the fully general problem is so easy and so useful to solve it's exasperating.
I guess I should stop complaining and submit a patch to grep to add a `--dont-use-linefeed-instead-use <arg>` option already.
E.g.,
# gron
gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" |
fgrep commit.author
json[0].commit.author = {};
json[0].commit.author.date = "2016-07-02T10:51:21Z";
json[0].commit.author.email = "mail@tomnomnom.com";
json[0].commit.author.name = "Tom Hudson";
# jq
curl -L "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" |
jq -r 'paths(scalars) as $p | getpath($p) | "\($p|join(".")|select(contains("commit.author"))) = \(.)"'
0.commit.author.name = Tom Hudson
0.commit.author.email = mail@tomnomnom.com
0.commit.author.date = 2022-04-13T14:23:37Z
# jq with grep outside jq
curl -L "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" |
jq -r 'paths(scalars) as $p | getpath($p) | "\($p|join(".")) = \(.)"' |
fgrep commit.author
0.commit.author.name = Tom Hudson
0.commit.author.email = mail@tomnomnom.com
0.commit.author.date = 2022-04-13T14:23:37Z
With just a bit more work you can get it to output valid gron, and even to parse valid gron.the biggest usecase for me is taking some csv, toml, xml, whatever and converting that to json so I can pipe to jq
It's inspired by XPath so it's very familiar instead of a complete new DSL. The killer feature imo is the recursive key lookup so you can write `people..address` and it'll find all "address" keys that descend from "people" anywhere in the JSON. It's by far my favorite parsing language for JSON and I wrote an introduction blog on how to use it in JSON dataset parsing [2] :)