Skip to content

Instantly share code, notes, and snippets.

@robmiller
Last active August 29, 2015 14:19
Show Gist options
  • Save robmiller/ce072c37df914adec0ac to your computer and use it in GitHub Desktop.
Save robmiller/ce072c37df914adec0ac to your computer and use it in GitHub Desktop.
Ruby one-liner to replace URLs in a CSV with their bitly-shortened versions. Assumes you have a bit.ly account
$ gem install bitly
$ export BITLY_USERNAME="your bit.ly username" BITLY_API_KEY="your bit.ly API key"
$ ruby -rcsv -rbitly -e 'b = Bitly.new(ENV["BITLY_USERNAME"], ENV["BITLY_API_KEY"]); CSV.filter { |r| r.each { |f| f.replace b.shorten(f).short_url if f =~ /^https?:/ } }' urls.csv > urls.shortened.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment