Roger Lipscombe
Roger Lipscombe

Reputation: 91885

Using select with reduce in jq?

If I have a simple reduce expression:

reduce inputs as $line
([]; . + [$line])

(this is roughly equivalent to --slurp: ignore that; it's just an example)

...but I want to filter inputs somehow.

Where do I put the select?

Upvotes: 0

Views: 780

Answers (2)

peak
peak

Reputation: 116870

In the particular case given, a simpler solution is:

[inputs | select(...)]

In fact, it often happens that a solution using reduce can be simplified, e.g. as here or by using one of the built-in reduction filters (notably add).

Of course, when using inputs to avoid slurping, the -n command-line option is needed.

Upvotes: 0

Kamal
Kamal

Reputation: 2554

I am not sure that I understand your question completely, but I guess this should work

reduce (inputs | select(Your_filter)) as $line
([]; . + [$line])

Upvotes: 2

Related Questions