4 ms·
I believe a good fit for Unix commands is using a pipeline where the output of your command (say, ls) is converted to JSON, so that later in the pipeline you ca
by gonzus 6y ago
I believe a good fit for Unix commands is using a pipeline where the output of your command (say, ls) is converted to JSON, so that later in the pipeline you can use jq to filter / count / etc in a structured, non-whitespace-dependent way. A combination of jc (https://github.com/kellyjonbrazil/jc https://github.com/kellyjonbrazil/jc) and jq fits the bill for me:
ls -l /usr/bin | jc --ls | jq '.[] | select(.size > 50000000)'
- lordgroff 6y agoI think this should honestly be higher. I'm not sure if jc is THE tool -- the Python dependency irks me slightly -- but I like the simplicity of the idea, wrapping around what is already available and functional, while improving my experience. I plan to play around with this idea.
- kbrazil 6y agojc is also available as a standalone binary with python 'packaged in'. https://kellyjonbrazil.github.io/jc-packaging/ https://kellyjonbrazil.github.io/jc-packaging/
- pedro84 6y ago$ ls -l /usr/bin | awk '$5 > 50000000' -rwxr-xr-x 1 root root 51859776 Jan 26 2018 pandoc
- kbrazil 6y agocan you do that with netstat? :) (ls was one of the more simple parsers to write, aside from some corner cases)
- Skunkleton 6y agoMost commands that you would want to put into a pipeline already have some sort of machine parsable output flag (tabular, json, format specifier, etc). Still, there is not much reason to call ls from a script at all. In python you would be better off with the native libraries. In bash, you would probably be best off with `find /usr/bin -maxdepth 1 -size +50M` in your usecase. IMO people have been trying to do the same thing with these tools since they were invented. Better to learn how to use them the way they were intended before reinventing them. Edit: re-read my comment. Seems a little negative. Sorry. To be clear, I think the jc/jq approach is much better than the roll-your-own parser in shell approach.
- kbrazil 6y agoOne use case that has come up is to parse the output of commands your run on remote hosts that may not even have python installed. I have a PR into Ansible community.general plugins to add this functionality and it works pretty sweet. There are some other automation tools that are looking into this approach as well.