#!/bin/bash # Take a CSV file with a header row and create a SQL table containing named fields # Then import the data into the table. if [ -z "$2" ] then echo "Please supply a CSV filename with a header row, and a table name to be created from it" exit 1 fi csvfile="$1" table="$2" DB=MyDatabaseName length=64 ( echo "drop table if exists $table;" echo "create table $table (UniqueIndex int auto_increment unique" head -n1 "$csvfile" | fromdos | sed 's/^"//; s/","/\n/g; s/"$//' | while read field do sqlfield=`echo "$field" | tr -cd '[:alpha:]'` echo ",$sqlfield varchar($length)" done echo ");" ) | mysql $DB tail -n+2 "$csvfile" | fromdos | sed "s/\(.*\)/insert into $table values (0,\1);/" | mysql $DB