Shell export MySQL data into CSV format

Source: Internet
Author: User

#!/bin/bashfunction usage {       echo  "Usage: $0  [options]              -h   host --optional             -p   port --optional             -u   user             -p   Password             -d  dbname              -t  tablename              -o  outputfile "}if [  $# -lt 1 ]then     usageelse     while   getopts  "H:p:u:p:D:t:o: " opt;     do        case ${ OPT}&NBSP;IN&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;H)  host=${OPTARG}         ;;         o)  outfile=${OPTARG}         ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;P)  port=${OPTARG}         ;;         u)  user=${OPTARG}         ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;P)  passwd=${OPTARG}         ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;D)  dbname=${OPTARG}         ;; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;T)  tablename=${OPTARG}         ;;         esac        donefihost=${host:-localhost}port=${ Port:-3306}mysql= "MYSQL&NBSP;-H&NBSP;${HOST}&NBSP;-P&NBSP;${PORT}&NBSP;&NBSP;-U&NBSP;${USER}&NBSP;-P${PASSWD}  -ane "query=" Select column_name, column_type, is_nullable, column_comment from  information_schema.columns WHERE table_schema =  ' ${dbname} '  and table_name  =  ' ${tablename} ' "Value=$ (${mysql} " ${query} ") echo  ' ${value} ' |awk   ' {out= '";  for  (i=4; i<=nf;i++) {out=out " " $i};p rint $1 "\ t" $ "t" $ "\ T" OutFile ".csv#echo " ${value} "|awk   ' {out=" "; for  (i=4; i<=nf;i++) {out=out"   "$i};p rint $1", "$ $", "$ $", "out}"  >> test1.csv


This article from "The Dark before Dawn" blog, declined reprint!

Shell export MySQL data into CSV format

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.