I have problem with cleaning comments and empty lines from already existing sql file. The file has over 10k lines so cleaning it manually is not an option.
I have a litt
It is possible to do it with regular expressions. First you have to split the file by strings and after this you can split the file by comments. The following Perl program does it:
#! /usr/bin/perl -w
# Read hole file.
my $file = join ('', <>);
# Split by strings including the strings.
my @major_parts = split (/('(?:[^'\\]++|\\.)*+')/, $file);
foreach my $part (@major_parts) {
if ($part =~ /^'/) {
# Print the part if it is a string.
print $part;
}
else {
# Split by comments removing the comments
my @minor_parts = split (/^--.*$/m, $part);
# Print the remaining parts.
print join ('', @minor_parts);
}
}