Skip to main content

Posts

Showing posts with the label Shell Script

Shell Script to export MySQL table data into a text file

Hi, our today's topic is " Write Shell Script to export MySql table data into a text file ". I will try my best to explain each and everything in steps. Let's start : Following is full shell script #tableexport.sh   #(1) define variables DBNAME=test FNAME=/usr/tmp/$(date +%Y.%m.%d)-$1.txt #(2)creates an file with column names seperated by comma in a single line mysql -u$2 -p$3 $DBNAME -B -e "SELECT COLUMN_NAME FROM information_schema.COLUMNS C WHERE table_name = '$1';" | awk '{print $1}' | grep -iv ^COLUMN_NAME$ | sed 's/^/"/g;s/$/"/g' | tr '\n' ',' > $FNAME #(3)appends newline to mark beginning of data vs. column titles echo "" >> $FNAME #(4)dumps data from DB into /usr/tmp/tempfile.txt mysql -u$2 -p$3 $DBNAME -B -e "SELECT * INTO OUTFILE '/usr/tmp/tempfile.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM $1;...