Deano
Deano

Reputation: 12190

awk create a bulk MySQL insert from csv

I have a huge CSV file with data, I'm trying to generate mySQL bulk insert statement.

Data presented as follows in CSV

90927597|1356976813998|1356976814177|1356976817457|17756249959|17756249959|18663111085|17753220005|1090917|1775624995900|A
90927599|1356976813098|1356976814797|1356976823738|12562175250|12562175250|12566502514|12565207040|1890362|1256217525000|A
90927602|1356976813098|1356976814797|1356976823738|12562175250|12562175250|12566502514|12565207040|1890362|1256217525000|A
90927603|1356976813098|1356976814797|1356976823738|12562175250|12562175250|12566502514|12565207040|1890362|1256217525000|A

I'm looking to create a bulk insert that will combine every 2 rows into single insert statement

MySQL statement with desired output

insert into data (90927597,1356976813998,1356976814177,1356976817457,17756249959,17756249959,18663111085,17753220005,1090917,1775624995900,A,90927599,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);
insert into data (90927602,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A, 90927603,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);

Thank you

Upvotes: 0

Views: 1491

Answers (2)

Ed Morton
Ed Morton

Reputation: 203502

$ cat tst.awk
BEGIN{ FS="|"; OFS="," }
{
    $1 = $1
    head = ( (NR%2) == 1 ? tail "insert into data (" : "," )
    printf "%s%s", head, $0
    tail = ");\n"
}
END { printf "%s", tail }

$ awk -f tst.awk file
insert into data (90927597,1356976813998,1356976814177,1356976817457,17756249959,17756249959,18663111085,17753220005,1090917,1775624995900,A,90927599,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);
insert into data (90927602,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A,90927603,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);

Upvotes: 3

Chris Seymour
Chris Seymour

Reputation: 85785

With awk:

$ awk 'BEGIN{printf "%s","insert into date ("}$NF{gsub(/\|/,",");printf "%s",$0}END{print ");"}' file
insert into date (90927597,1356976813998,1356976814177,1356976817457,17756249959,17756249959,18663111085,17753220005,1090917,1775624995900,A90927599,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);

A more concise way using tr:

$ echo "insert into date ($(tr -d '\n' < file));" | tr '|' ','
insert into date (90927597,1356976813998,1356976814177,1356976817457,17756249959,17756249959,18663111085,17753220005,1090917,1775624995900,A90927599,1356976813098,1356976814797,1356976823738,12562175250,12562175250,12566502514,12565207040,1890362,1256217525000,A);

Upvotes: 1

Related Questions