Last active
December 29, 2015 15:39
-
-
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.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[ ${#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