Last active
January 4, 2016 18:59
-
-
Save ryanbrainard/8664679 to your computer and use it in GitHub Desktop.
Script for extracting a single column from a Heroku Dataclip. Designed for piping into commands like xargs. Requires jq be installed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
error() { | |
printf "$1\n" >&2 | |
exit 1 | |
} | |
[[ $# -eq 0 ]] && error "Usage: `basename $0` hash column [row(s)]" | |
hash=${1?'Must provide Dataclip hash'} | |
[[ $hash =~ ^http ]] && error 'Provide only Dataclip hash, not full URL' | |
url="https://dataclips.heroku.com/${hash}.json" | |
col=${2?'Must provide column index'} | |
[[ $col =~ [0-9]+ ]] || error 'Column index must be a number' | |
rows=$3 | |
[[ $rows =~ ^[0-9]*:[0-9]*$ ]] && rows_selector="[$rows][]" | |
[[ $rows =~ ^[0-9]+$ ]] && rows_selector="[$rows]" | |
[[ -z $rows ]] && rows_selector="[]" | |
[[ -z $rows_selector ]] && error "Rows must be specified as a single number or range. e.g. 2 or 2:5 or 2: or :5" | |
curl --fail --silent --location $url | jq --raw-output ".values${rows_selector}[${col}]" | |
pipestatus="${PIPESTATUS[*]}" | |
[[ $pipestatus = "22 0" ]] && error "404: Dataclip not found." | |
[[ $pipestatus = "0 0" ]] || error "Unknown error.\nURL: ${url}\nRow Selector: ${rows_selector}\nColumn: ${col}\nError codes: ${pipestatus}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moved to https://github.com/heroku/dataclips-cli