Решение Louis L. у меня не работало, поэтому я протестировал это решение gawk с дампами из sqlite3 версии 3.8.7.1
операторы таблицы CREATE похожи, например, на
CREATE TABLE "strom" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"otec" integer NOT NULL,
"nazev" text NOT NULL,
"ikona" text NULL,
"barva" text NULL
);
но также может выглядеть так
CREATE TABLE "changes" (
"version" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"last_change" text NOT NULL DEFAULT (datetime('now','localtime')),
`ref` text NOT NULL,
"ref_id" text NULL,
"action" text NOT NULL
, "data" text NOT NULL DEFAULT '');
#!/usr/bin/gawk -f
# input is sqlite3 dump, tested with sqlite3 version 3.8.7.1
# output are INSERT statements including column names
# i.e. not e.g.
# INSERT INTO "changes" VALUES(1,'2016-07-19 17:46:12','cenik','10','UPDATE','');
# like in standard dump
# but
# INSERT INTO "changes" ("version", "last_change", "ref", "ref_id", "action", "data") VALUES(1,'2016-07-19 17:46:12','cenik','10','UPDATE','');
# BEGIN TRANSACTION and COMMIT are included in output
BEGIN {
state = "default" # default/definition/insert let us know wether we are in CREATE or INSERT statement
print_definitions = 0 # wether to print CREATE statements or not
}
state == "default" && match($0, /^CREATE TABLE \"([A-Za-z0-9_]+)\" *\($/, a) {
tablename = a[1]
state = "definition"
if (print_definitions)
print
next
}
state == "definition" && /^ *); *$/ {
state = "default"
if (print_definitions)
print
next
}
state == "definition" && ! ( /^[\ ]{1,2}PRIMARY/ || /UNIQUE/ || /CHECK/ || /^[\ ]{1,2}FOREIGN KEY.*REFERENCES/) {
if (length(columnlist [tablename]))
columnlist[tablename] = columnlist[tablename] ", "
if (match($0, /(\".*\")/, b))
columnlist[tablename] = columnlist[tablename] b[1]
if (match($0, /`(.*)`/, c))
columnlist[tablename] = columnlist[tablename] "\""c[1]"\""
if (print_definitions)
print
}
state == "definition" && /^.*); *$/ {
state = "default"
next
}
state == "default" && match($0, /^(INSERT INTO ")([A-Za-z0-9_]+)"(.*)/, a) {
print a[1] a[2] "\" (" columnlist[a[2]] ")" a[3]
state = "insert"
if (/^.*); *$/)
state = "default"
}
state == "insert" && ! /^INSERT INTO/{
print
}
state == "insert" && /^.*); *$/ {
state = "default"
next
}
state == "default" && (/^ *BEGIN TRANSACTION;/ || /^ *COMMIT;/) {
print
}