Skip to content

Instantly share code, notes, and snippets.

@wader
Created March 14, 2021 14:46
Show Gist options
  • Save wader/b8da6b7e4fcb2202358f53cf35819380 to your computer and use it in GitHub Desktop.
Save wader/b8da6b7e4fcb2202358f53cf35819380 to your computer and use it in GitHub Desktop.
jq csv and csv kv and range map
def csv: split(",");
def csv_kv: [csv[] | split("=") | {(.[0]): .[1]}] | add;
def obj_to_csv_kv: [to_entries[] | [.key, .value] | join("=")] | join(",");
# TODO: errors?
def csv_range_map:
# "0-2=a,1=b" -> ["a", "b", "a"]
[ csv[]
| split("=")
| .[1] as $c
| .[0]
| split(":")[]
| split("-")
| map(tonumber)
| if (.[1] | not) then [.[0], .[0]] end
| range(.[0]; .[1]+1)
| [., $c]
]
| reduce .[] as $r ([]; .[$r[0]] = $r[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment