Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active December 29, 2015 15:39
Show Gist options
  • Save coderofsalvation/7692376 to your computer and use it in GitHub Desktop.
Save coderofsalvation/7692376 to your computer and use it in GitHub Desktop.
execute an sqlquery and output as csv (handy for generating quikcndirty sql reports e.g.)
[[ ${#3} == 0 ]] && echo "Usage: [PASSWORD=""] $0 <dbname> <username> <query>" && exit 11
db="$1"
user="$2"
query="$3"
# execute query, quote quotes, and delimit with ","
mysql -u"$user" -p"$PASSWORD" "$db" -B -e "set names 'utf8'; $query" | \
sed -e 's/"/\\"/g' |
sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' | sed ':a;N;s/\r//g;s/\\n//g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment