IT序号网

shell根据csv生成sql知识解答

lxf 2021年05月25日 程序员 140 0

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');


发布评论
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

shell中的EOF用法知识解答
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。