Live data from Hacker News

Parsing JSON in Forty Lines of Awk

akr.am

11–20 of 62 posts

Re: Parsing JSON in Forty Lines of Awk

#11
post #8

JSON is not a friendly format to the Unix shell — it’s hierarchical, and cannot be reasonably split on any character Yes, shell is definitely too weak to parse JSON! (One reason I started https://oils.pub is because I saw that bash completion scripts try to parse bash in bash, which is an even worse idea than trying to parse JSON in bash) I'd argue that Awk is ALSO too weak to parse JSON The following code assumes th…

I don't really buy that shell / awk is "too weak" to deal with JSON, the ecosystem of tools is just fairly immature as most of the shells common tools predate JSON by at least a decade. `jq` being a pretty reasonable addition to the standard set of tools included in environments by default.

IMO the real problem is that JSON doesn't work very well at as a because it's core abstraction is objects. It's a pain to deal with in pretty much every statically typed non-object oriented language unless you parse it into native, predefined data structures (think annotated Go structs, Rust, etc.).

Re: Parsing JSON in Forty Lines of Awk

#12
post #8

JSON is not a friendly format to the Unix shell — it’s hierarchical, and cannot be reasonably split on any character Yes, shell is definitely too weak to parse JSON! (One reason I started https://oils.pub is because I saw that bash completion scripts try to parse bash in bash, which is an even worse idea than trying to parse JSON in bash) I'd argue that Awk is ALSO too weak to parse JSON The following code assumes th…

> Yes, shell is definitely too weak to parse JSON!

Parsing is a trivial, rejecting invalid input is trivial, the problem is representing the parsed content in a meaningful way.

> bash completion scripts try to parse bash in bash

You're talking about ble.sh, right? I investigated it as well.

I think they made some choices that eventually led to the parser being too complex, largely due to the problem of representing what was parsed.

> Also, OSH is now FASTER than bash, in both computation and I/O.

According to my tests, this is true. Congratulations!

Re: Parsing JSON in Forty Lines of Awk

#13
post #3

Awk is great and this is a great post. But dang, awk really shoots itself so much with its lack of features that it so desperately needs! Like: printing all but one column somewhere in the middle. It turns into long, long commands that really pull away from the spirit of fast fabrication unix experimentation. jq and sql both have the same problem :)

$ echo "one two three four five" | awk '{$3="";print}' one two four five

Oh dang, that's good.

Re: Parsing JSON in Forty Lines of Awk

#14
post #9
post #3

Awk is great and this is a great post. But dang, awk really shoots itself so much with its lack of features that it so desperately needs! Like: printing all but one column somewhere in the middle. It turns into long, long commands that really pull away from the spirit of fast fabrication unix experimentation. jq and sql both have the same problem :)

>awk really shoots itself so much with its lack of features that it so desperately needs! That's why I use Perl instead (besides some short one liners in awk, which in some cases are even shorter than the Perl version) and do my JSON parsing in Perl. This diff -rs a/ b/ | ask '/identical/ {print $4}' | xargs rm is one of my often used awk one liners. Unless some filenames contain e.g. whitespace, then it's Perl again

This is much safer: xargs -d '\n' rm -f --

Re: Parsing JSON in Forty Lines of Awk

#15
post #14
post #9

Earlier quoted context omitted.

>awk really shoots itself so much with its lack of features that it so desperately needs! That's why I use Perl instead (besides some short one liners in awk, which in some cases are even shorter than the Perl version) and do my JSON parsing in Perl. This diff -rs a/ b/ | ask '/identical/ {print $4}' | xargs rm is one of my often used awk one liners. Unless some filenames contain e.g. whitespace, then it's Perl again

This is much safer: xargs -d '\n' rm -f --

Sure, but my example was just that and I actually use /identical$/ as the pattern. Sorry for the typo.

And I use this "historic" one liner only when I know about the contents of both directories. As soon as I need a "safer" solution I use a Perl script and pattern matching, as I said.

Re: Parsing JSON in Forty Lines of Awk

#17
I feel like the takeaway here is that jq should probably be considered an indispensable part of the modern shell environment. Because without it, you can't sensibly deal with JSON in a shell script, and JSON is everywhere now. It's cool that you can write a parser in awk but the point of this kind of scripting language is to not have to do things like that.

Re: Parsing JSON in Forty Lines of Awk

#18
post #3

Awk is great and this is a great post. But dang, awk really shoots itself so much with its lack of features that it so desperately needs! Like: printing all but one column somewhere in the middle. It turns into long, long commands that really pull away from the spirit of fast fabrication unix experimentation. jq and sql both have the same problem :)

> awk really shoots itself so much with its lack of features that it so desperately needs Whence perl.

or raku

https://github.com/moritz/json/blob/master/lib/JSON/Tiny/Gra...

Re: Parsing JSON in Forty Lines of Awk

#20
post #9
post #3

Awk is great and this is a great post. But dang, awk really shoots itself so much with its lack of features that it so desperately needs! Like: printing all but one column somewhere in the middle. It turns into long, long commands that really pull away from the spirit of fast fabrication unix experimentation. jq and sql both have the same problem :)

>awk really shoots itself so much with its lack of features that it so desperately needs! That's why I use Perl instead (besides some short one liners in awk, which in some cases are even shorter than the Perl version) and do my JSON parsing in Perl. This diff -rs a/ b/ | ask '/identical/ {print $4}' | xargs rm is one of my often used awk one liners. Unless some filenames contain e.g. whitespace, then it's Perl again

I've been using perl instead of sed because PCRE is just better and it's the same regex that PHP uses which I've been coding in for nearly 20 years. I still don't actually know perl, but apparently Gemini does. It wrote a particularly crazy find and replace for me. Never got around to using or learning awk. Only time I see it come up is when you want to parse some tab delimited output
Post reply on HN