Если вы получаете многострочные вставки, вам нужно использовать что-то более гибкое, чем grep. Пример использования perl однострочной:
perl -wlne 'if (!$current_statement){if (/^\s*INSERT/){if (/;\s*$/) {push @inserts, $_} else {s/\r//g;$current_statement = $_}}} else {$current_statement .= $_;if (/;\s*$/){push @inserts, $current_statement;$current_statement = "";}}}{print for (@inserts)' /tmp/export_data.sql > /tmp/insert_data.sql
Разбивка того, что происходит:
# - w: use warnings
# - l: loop through input storing each line's contents in $_
# - n: don't print the line after processing
# - e: execute one liner
if (!$current_statement) { # if not in multi-line insert
if (/^\s*INSERT/) { # skip if it's not an INSERT statement
if (/;\s*$/){ # single line insert, push to list
push @inserts, $_
} else { # multi-line insert strip off CR, save line and continue
s/\r//g;
$current_statement = $_
}
}
} else { # continue an multi-line existing multi-line insert
$current_statement .= $_; # always append the statement
if (/;\s*$/){ # if finished, push to @inserts, reset variable
push @inserts, $current_statement;
$current_statement = "";
}
}
}{ # "eskimo kiss": finishes the loop from -n, runs code after processing the files
print for (@inserts) # print each entry of the inserts array