简体   繁体   中英

Is there anyway to write a linux script that reads the csv file and create the sql file?

I want to write a script that reads the csv file and create the sql file? So I have several data in file.csv files like:

Ball,Jamin,1414 Willow Rd.,Cupertino,CA,94024
Church,Joe,2500 Main St.,Los Altos,CA,94023
Foothill,Ann,12345 El Monte Rd.,Los Altos,CA,94022

And I want to write a code in Linux to get the csv file like above and create the file.sql code so I can use that sql file for insert purpose. For example, I want the code will be like this in .sql file

INSERT INTO students (lname,fname,address,city,state,zip) VALUES
('Ball', 'Jamin',  '1414 Willow Rd.',  'Cupertino',  'CA',  '94024');
INSERT INTO students (lname,fname,address,city,state,zip) VALUES
('Church', 'Joe',  '2500 Main St.', 'Los Altos',  'CA',  '94023');
INSERT INTO students (lname,fname,address,city,state,zip) VALUES
('Foothill', 'Ann', '12345 El Monte Rd.',  'Los Altos', 'CA',  '94022');

So for example, I say,

./generateSQL.sh file.csv

and it will generate and (create) and save the .sql file. Is it possible?

Here is what I have done so far:

awk -F',' '{ print "insert into " $1 " VALUES(" $2 ", " $3 ", " $4 ", " $5 ", "  $6 ", " $7 ");" }' input.csv > output.log

My Code is only works for first line and I actually want this happen till all lines generated to SQL and I want this as a Generate.sh file that I can do below in my Linux. Not sure how:

./generateSQL.sh file.csv

Bash loop

while IFS=',' read -r lname fname address city state zip; do
    printf "INSERT INTO students (lname,fname,address,city,state,zip) VALUES\n"
    printf "('%s', '%s', '%s', '%s', '%s', %s');\n" "$lname" "$fname" "$address" "$city" "$state" "$zip"
done <<EOF
Ball,Jamin,1414 Willow Rd.,Cupertino,CA,94024
Church,Joe,2500 Main St.,Los Altos,CA,94023
Foothill,Ann,12345 El Monte Rd.,Los Altos,CA,94022
EOF

Slow, but flexible.

Awk script

awk -F',' '{ 
    print "INSERT INTO students (lname,fname,address,city,state,zip) VALUES"
    print "('\''" $1 "'\'', '\''" $2 "'\'', '\''" $3 "'\'', '\''" $4 "'\'', '\''"  $5 "'\'', '\''" $6 "'\'');" }' <<EOF
Ball,Jamin,1414 Willow Rd.,Cupertino,CA,94024
Church,Joe,2500 Main St.,Los Altos,CA,94023
Foothill,Ann,12345 El Monte Rd.,Los Altos,CA,94022
EOF

Faster, but external tool with it's own syntax. Also quoting here is pain, but it can be reduced by having the script as an awk script with awk shebang:

cat <<'EOF' >script.awk
#!/bin/awk -f
BEGIN {
    FS=","
}
{
    print "INSERT INTO students (lname,fname,address,city,state,zip) VALUES"
    print "('" $1 "', '" $2 "', '" $3 "', '" $4 "', '"  $5 "', '" $6 "');"
}
EOF

chmod +x script.awk

./script.awk <<EOF
Ball,Jamin,1414 Willow Rd.,Cupertino,CA,94024
Church,Joe,2500 Main St.,Los Altos,CA,94023
Foothill,Ann,12345 El Monte Rd.,Los Altos,CA,94022
EOF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM