the simplest solution is the add
filter:
jq '[.duration] | add'
the [
brackets ]
are needed around the value to sum because add
sums the values of an array, not a stream. (for stream summation, you would need a more sophisticated solution, e.g. using reduce
, as detailed in other answers.)
depending on the exact format of the input, you may need some preprocessing to get this right.
e.g. for the sample input in Charles Duffy’s answer either
-
use
inputs
(note that-n
is needed to avoid jq swallowing the first line of input):jq -n '[inputs.duration] | add' <<< "$sample_data"
-
or slurp (
-s
) and iterate (.[]
) / map:jq -s '[.[].duration] | add' <<< "$sample_data" jq -s 'map(.duration) | add' <<< "$sample_data"