Skip to content

Instantly share code, notes, and snippets.

@zeke
Forked from ryanbrainard/dataclip-column
Created January 28, 2014 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeke/8675558 to your computer and use it in GitHub Desktop.
Save zeke/8675558 to your computer and use it in GitHub Desktop.
#!/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