Skip to content

Instantly share code, notes, and snippets.

@ryanbrainard
Last active January 4, 2016 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ryanbrainard/8664679 to your computer and use it in GitHub Desktop.
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.
#!/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}"
@ryanbrainard
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment