3 ms·
I'd like to give a shoutout to jq for (non-JSON) text processing, and also as an almost-replacement for awk: echo "foo bar" | jq -Rr 'split(" ")[0]' # prints
by penguin_booze 1y ago
I'd like to give a shoutout to jq for (non-JSON) text processing, and also as an almost-replacement for awk:
echo "foo bar" | jq -Rr 'split(" ")[0]' # prints foo
I say 'almost', because jq right now can't invoke sub processes from within, unlike awk and bash. But otherwise, it's a fully fledged, functional, language.
- wwader 1y agoSame! and jq's regexp functions are quite powerful for transforming text into something more structured: $ echo -e "10:20: hello\n20:39: world" | jq -cR 'capture("^(?<timestamp>.*): (?<message>.*)$")' {"timestamp":"10:20","message":"hello"} {"timestamp":"20:39","message":"world"} $ echo -e "10:20: hello\n20:39: world" | jq -Rn '[inputs | capture("(?<t>.*): (?<m>.*)$").m] | join(" ")' "hello world" Also using inputs/0 etc in combination with reduce and foreach it's possible to process streams of text that might not end.
- Eduard 1y ago`jq -Rr 'split(" ")[0]'`? That would easily take me three decades to memorize ;-). for selecting the second column, PID, from `ps aux`, ps aux | awk '{print $2}' works for me, as gawk's default $FS field separator treats runs of whitespaces as a single separation of fields ( https://www.gnu.org/software/gawk/manual/html_node/Field-Splitting-Summary.html https://www.gnu.org/software/gawk/manual/html_node/Field-Spl... ), quite similar to "standard" Open Group/POSIX awk ( https://pubs.opengroup.org/onlinepubs/9799919799/utilities/awk.html#tag_20_06_13_04 https://pubs.opengroup.org/onlinepubs/9799919799/utilities/a... ). ps aux | jq -Rr 'split(" ")[1]' on the other hand doesn't work here due to `ps aux` adding a variable number of spaces for formatting. ps aux | jq -Rr 'split("\\s+"; null)[1]' seems to work though.
- penguin_booze 1y agoYes, in awk, conveniently, whitespace means a run of whitespace. It gets a bit verbose in jq for the same effect; but on the other hand, you get slicing - more slices than `cut` can give you.