3 ms·
Ever tried to parse XML or json in linux shell? It's not a matter of familiarity with linux shell. It's a matter of wasting time debugging/implementing stuff i
by ajuc 6y ago
Ever tried to parse XML or json in linux shell?
It's not a matter of familiarity with linux shell. It's a matter of wasting time debugging/implementing stuff in shell that is trivial to implement in any modern scripting language.
- freedomben 6y agoHaven't parsed XML in quite a number of years but I parse JSON to extract values from a curl command all the time with jq and it's really not that bad. If there's anything more than simple extraction required then I agree it's not good for the shell. For that I turn to the primary language of the project preferably (which these days is Elixir and sometimes Ruby for me). But even JSON and (X|HT)ML you'd be amazed how often a simple grep can snag what you want, and regular expressions are also mostly universal so nearly everybody can read them.
- ajuc 6y agoWe (2-person team doing embedded QT app on linux) did it the exact way you're arguing for - starting with shell, struggling with it for several months every time something went wrong or we had to change it, and finally giving up and rewriting it in python. Python wasn't our main language (the app was in C++ and QML which is basically java script). But we both knew Python and it's the easiest language for these sort of things that we both knew and that comes with the system. My main conclussion is - I'm starting with python the next time. Life's too short to check for the number of arguments in each function.
- verall 6y agoI think there is a big difference between this and the parent. If you are working on projects where the "primary language" is Ruby or Python or Elixr than sure, use that, but if your project's primary language is C++, like most embedded applications, you do NOT want to use that. Any complex or cross-platform C++ projects will need a scripting language in addition to shell and a build system.
- chubot 6y agoLife's too short to check for the number of arguments in each function. Ha! The Oil shell does that for you :) https://www.oilshell.org/release/0.8.0/doc/idioms.html#use-procs-better-shell-functions https://www.oilshell.org/release/0.8.0/doc/idioms.html#use-p... (It's our upgrade path from bash)
- deleted 6y ago[deleted]
- reificator 6y agoI do JSON pretty frequently with jq and idiomatic python[0] can't even approach its ease of use. [0]: I'm sure there's a jq style library for python somewhere but it's definitely not the norm.
- tingletech 6y agothere are a couple of bindings from python to jq, I can't remember which I've used, but it makes it super simple to do jq queries from python
- jdc 6y agoJMESPath works pretty well: https://github.com/jmespath/jmespath.py https://github.com/jmespath/jmespath.py
- deleted 6y ago[deleted]
- ak217 6y agoShameless plug: my project yq (https://github.com/kislyuk/yq https://github.com/kislyuk/yq) (pip install yq) includes a utility, xq, which transcodes XML to JSON using the Goessner JsonPath transformation (https://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html https://www.xml.com/pub/a/2006/05/31/converting-between-xml-...) so you can use jq to query XML.
- kasabali 6y agoIf you're trying to implement stuff in shell you're doing it wrong. It's a glue language. and yes, parsing XML or JSON in "linux shell" is very easy. For JSON use jq, for XML use xml2 or xmlstarlet.
- anon73044 6y ago>json in linux shell? Yes! Fortunately for me a 10 whole lines of sed, awk and some trimming and a for loop turned my json into a CSV to pass back to a legacy system. No curl or json modules necessary. That being said, if I needed anything more complex Python would be my default.