shell根据csv生成sql

member.csv

Blum,Richard,123 Main St.,Chicago,IL,60601
Blum,Barbara,123 Main St.,Chicago,IL,60601
Bresnahan,Christine,456 Oak Ave.,Columbus,OH,43201
Bresnahan,Timothy,456 Oak Ave.,Columbus,OH,43201

 

csv2sql.sh

#!/bin/bash
# read file and create INSERT statement for MYSQL

outfile="mem.sql"
IFS=","
while read lname fname address city state zip
do
  cat >> $outfile << EOF
  INSERT INTO members (lname, fname, address, city, state, zip) VALUES
  ('$lname', '$filename', '$address', '$city', '$state', '$zip');
EOF
done < ${1}

执行后生成

INSERT INTO members (lname, fname, address, city, state, zip) VALUES
  ('Blum', '', '123 Main St.', 'Chicago', 'IL', '60601');
  INSERT INTO members (lname, fname, address, city, state, zip) VALUES
  ('Blum', '', '123 Main St.', 'Chicago', 'IL', '60601');
  INSERT INTO members (lname, fname, address, city, state, zip) VALUES
  ('Bresnahan', '', '456 Oak Ave.', 'Columbus', 'OH', '43201');
  INSERT INTO members (lname, fname, address, city, state, zip) VALUES
  ('Bresnahan', '', '456 Oak Ave.', 'Columbus', 'OH', '43201');

 

喜欢这篇文章?欢迎打赏~~

 

posted @ 2020-03-08 20:10  苍青浪  阅读(777)  评论(0编辑  收藏  举报