On Sun, Nov 07, 2021 at 04:39:34PM -0300, Rafael Azevedo wrote:
> This is the kind of knowledge that comes from Mars...
The `jq` interpreter is a rather handy tool. It is is worth learning if
you spend enough time working with JSON data. No Martian passport
required, just enough incentive and curiousity to make the effort.
> > jq -nr --argjson days "$days" ' # Bind $days via command line
> > (now - 86400 * $days) as $too_old # Bind $too_old to cutoff time
> > | $ARGS.positional as $qs # Bind argv ("$@") as $qs
> > | inputs # Input stream
> > | select(.arrival_time < $too_old) # Prune young messages
> > | first(select($qs == [] # Prune unless queue match
> > or { ( .queue_name , "all" ): true # queue_name, all -> true
> > }[$qs[]])) # indexed with each of $qs
> > | .queue_id # Grab just the queue_id
> > | select(test("^\\w+$")) # Make sure it is safe
> > ' --args -- "$@" # Set $ARGS.positional to "$@"
The only subtle bits above are:
0. `jq` streams are analogous to Python generators, but they're much
more central in the design of `jq`, everything works on streams,
what isn't a stream is merely a stream with one element.
1. `{ (stream): val }` creates an object in which each of the keys
generated by `stream` takes the value `val`.
2. `object[stream]` yields a stream of values obtained by using
each element of `stream` as a key into that object.
3. `expr | select(stream)` yields each value `expr` for each
value of `stream` that is neither `null` nor `false`.
4. `expr | first(select(stream))` outputs each value of `expr`
at most once (when at least value in `stream` was not
null or false).
The rest is just `jq` dialect for features you find in many other
languages. The novelty is using "|" for function composition, and
having streams as a fundamental abstraction.
[ And now, back to your regularly scheduled stream of Postfix topics ]
--
Viktor.