Skip to content

Instantly share code, notes, and snippets.

@wader
Created October 24, 2022 14:11
Show Gist options
  • Save wader/b2ddbbbdc6849ccadb18e5cff3a909be to your computer and use it in GitHub Desktop.
Save wader/b2ddbbbdc6849ccadb18e5cff3a909be to your computer and use it in GitHub Desktop.
# same as fromstream but with a prefix level, outputs [$prefix_path, $json_value], ...
# Usage:
# cat big | jq -L . --stream -n 'include "stream"; reduce fromstream(inputs; 1) as $pv ({}; $pv as [$p, $v] | .[$v.type] += 1)'
def streaks_by(f; g):
foreach ((g | ["v", .]), ["end"]) as $o (
{ acc: null
, fv: null
, emit: null
};
( .emit = null
| $o as [$t, $v]
| ($v | f) as $fv
| if $t == "end" then .emit = .acc
elif .acc == null then .acc = [$v]
else
if $fv == .fv then .acc += [$v]
else
( .emit = .acc
| .acc = [$v]
)
end
end
| .fv = $fv
);
.emit // empty
);
def fromstream(g; $level):
( streaks_by(
.[0][0:$level];
g
)
| [ .[0][0][0:$level]
, fromstream(.[] | .[0] |= .[$level:])
]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment