Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert massive MySQL dump file to CSV

Tags:

bash

mysql

sed

awk

I tryed something like this

awk -F " " '{if($1=="INSERT"){print $5}}' input.sql | \
    sed -e "s/^(//g" -e "s/),(/\n/g" -e "s/['\"]//g" \
        -e "s/);$//g" -e "s/,/;/g" > output.txt

But I find it slow and unoptimized

A MySQL dump file looks like the following

CREATE TABLE MyTable{
    data_1,
    data_2
};

INSERT INTO MyTAble VALUES ('data_1','data_2'),...,('data_1','data_2');
INSERT INTO MyTAble VALUES ('data_1','data_2'),...,('data_1','data_2');
...
INSERT INTO MyTAble VALUES ('data_1','data_2'),...,('data_1','data_2');

My goal is to get a file with the following result (and without ' or " to enclose fields):

data_1,data_2
data_1,data_2
...
data_1,data_2

Thanks in advance!

like image 599
Syffys Avatar asked Nov 01 '22 08:11

Syffys


2 Answers

You can try:

gawk '/^INSERT/ {
    match ($0,/[^(]*\(([^)]*)\)/,a)
    print a[1]
}' input.sql

* Update *

After reading the question again, maybe this is more what you want:

/^INSERT/ {
    line=$0
    while (match (line,/[^(]*\(([^)]*)\)/,a)) {
        cur=a[1]
        sub(/^['"]/,"",cur)
        sub(/['"]$/,"",cur)
        print cur
        line=substr(line,RSTART+RLENGTH)
    }
}

* Update 2 *

Based on the last update of the question, here is a new version:

/^INSERT/ {
    line=$0
    while (match (line,/[^(]*\(([^)]*)\)/,a)) {
        line=substr(line,RSTART+RLENGTH)
        match(a[1],/'([^']*)','([^']*)'/,b)
        print b[1]
        print b[2]
    }
}
like image 195
Håkon Hægland Avatar answered Nov 09 '22 07:11

Håkon Hægland


sed -n "/.*INSERT INTO MyTAble VALUES (\([^)]*\)).*/ {
   s/.*INSERT INTO MyTAble VALUES \(.*\);/\1/
   s/(\([^)]*\)),*/\\1\\
/g
   s/'//g
   s/\\n$//
   p
   }" input.sql > output.sql

based on your (new) sample

like image 36
NeronLeVelu Avatar answered Nov 09 '22 07:11

NeronLeVelu