Skip to content

Instantly share code, notes, and snippets.

@poeli
Last active March 12, 2020 05:07
Show Gist options
  • Save poeli/5ee4445955103407d9cc37581b7e014b to your computer and use it in GitHub Desktop.
Save poeli/5ee4445955103407d9cc37581b7e014b to your computer and use it in GitHub Desktop.
# USAGE: jq -R -s -f sacct2json.jq
# objectify/1 takes an array of string values as inputs, converts
# numeric values to numbers, and packages the results into an object
# with keys specified by the "headers" array
def objectify(headers):
# For jq 1.4, replace the following line by: def tonumberq: .;
def tonumberq: tonumber? // .;
. as $in
| reduce range(0; headers|length) as $i ({}; .[headers[$i]] = ($in[$i] | tonumberq) );
def sacct2table:
# For jq 1.4, replace the following line by: def trim: .;
def trim: sub("^ +";"") | sub(" +$";"");
split("\n") | map( split("|") | map(trim) );
def sacct2json:
sacct2table
| .[0] as $headers
| reduce (.[1:][] | select(length > 0) ) as $row
( []; . + [ $row|objectify($headers) ]);
sacct2json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment