Skip to content

Instantly share code, notes, and snippets.

@moriakijp
Created April 18, 2021 22:35
Show Gist options
  • Select an option

  • Save moriakijp/5db4bef6fb028a11493791abd60ca688 to your computer and use it in GitHub Desktop.

Select an option

Save moriakijp/5db4bef6fb028a11493791abd60ca688 to your computer and use it in GitHub Desktop.
jq '
def parsePathname:
split("\\") | {path: .[0:length-1], file: .[-1]};
# skip over lines that only specify directories
def pathnames:
foreach inputs as $x (null;
if . == null
then if ($x|length) == 0 then 0 else . end
else .+1
end;
select(. and . > 0)|$x)
| parsePathname ;
reduce pathnames as $pn ({};
getpath($pn.path + ["children"]) as $children
| setpath($pn.path + ["children"]; $children + [$pn.file]) );
. | pathnames'
dir.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment