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!
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]
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With