3

I'm trying to generate MySQL file from CSV data

90927744|1356976814398|0|1356976815339|18563182571|18563182571|18568753009|18563574221

is there is a way that I could use awk or sed to generate mysql insert statement as follows

insert into table (id,value,status,starttime,endtime,number,phone,number) values (90927744,1356976814398,0,1356976815339,18563182571,18563182571,18568753009,18563574221);

Thank you

3
  • using awk with field separator as | you can get all the fields separately. Then you need to relate 1st field to id, 2nd field to value, and so on.... Commented May 14, 2013 at 4:17
  • I tried to cat the file and pipe it to awk to add the "insert syntax, then the values between (), but I stumbled upon getting the syntax right Commented May 14, 2013 at 4:20
  • I have added an answer with examples...hope it helps. Commented May 14, 2013 at 4:32

5 Answers 5

3

To extract a particular field from your input....

$echo "90927744|1356976814398|0|1356976815339|18563182571|18563182571|18568753009|18563574221" |  awk -F "|" '{print $4}'
1356976815339

If you have one line in a.txt (see the script below for multiple lines in the input file)

$cat a.txt | awk -F "|" '{print $4}'

So to output the query you want--

$cat a.txt | awk -F "|" '{printf("insert into table (id,value,status,starttime,endtime,number,phone,number) values (%d, %d, %d, %d, %d, %d, %d, %d) \n", $1, $2, $3, $4, $5, $6, $7, $8)}'

If your file has multiple lines, then use the following script (or adapt this to your needs)

while read line           
do           
     echo "$line" | awk -F "|" ........
done < a.txt
Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest to use bound values instead of explicitly define the values. It makes slower if a lot of such lines are inserted. See topic about bind values in this related SO topic.
1

Just to refine the above written AWK statement, instead of while loop we can use

awk -F "|" '{printf("insert into table (id,value,status,starttime,endtime,number,phone,number) values (%d, %d, %d, %d, %d, %d, %d, %d) \n", $1, $2, $3, $4, $5, $6, $7, $8)}' a.txt >> result.txt

I hope this works

Comments

0

This might work for you (GNU sed):

sql="insert into table (id,value,status,starttime,endtime,number,phone,number) values"
sed 'y/|/,/;s/.*/'"$sql"' (&);/' file

Comments

0

You could use csvkit, see How to let csvkit/csvsql generate insert statements for csv file?

The statements will be for sqlite, the database engine behind csvkit, but conversion should be easy.

Comments

0

Although the question already is pretty old, someone might fall over it...

so another way of doing this is using standard ways for reading CSV files with LOAD DATA syntax which allows to use CSV files to read data and inserting it into a table:

LOAD DATA INFILE '/tmp/test.csv' INTO TABLE test FIELDS TERMINATED BY '|';

which takes the data from the CSV file and splits the valies on the | symbol.

1 Comment

FWIW, once loaded, you can then use mysqldump to create a .sql file. Not quite the same as generating inserts though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.