Skip to content

Instantly share code, notes, and snippets.

@lionelyoung
Created July 31, 2013 06:14
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 lionelyoung/6119739 to your computer and use it in GitHub Desktop.
Save lionelyoung/6119739 to your computer and use it in GitHub Desktop.
Replace urls in file with short urls using API from https://github.com/briancray/PHP-URL-Shortener
#!/usr/bin/bash
# Replace all long urls in MYFILE file with short urls using API from:
# https://github.com/briancray/PHP-URL-Shortener
MYFILE=~/somefile
DOMAIN=domain.com
URLSERVICE="http://$DOMAIN/shorten.php?longurl="
# Get URL from todo list
URLLIST=$(grep "http://" $MYFILE | sed 's/.*http:\/\/\([^ ]*\).*/http:\/\/\1/')
for url in $URLLIST; do
# Skip already short
if [[ "$url" == http://goo.gl/* ]]; then
continue
elif [[ "$url" == http://$DOMAIN/* ]]; then
continue
fi
# Pull short URL
API=${URLSERVICE}${url}
SHORTURL=`curl --silent $API`
echo Replacing $url with $SHORTURL
sed -i 's,'"$url"','"$SHORTURL"',' "$MYFILE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment