use strict; use warnings; while () { chomp; next if /^\s*--/; print $_,"\n" if !/--/; elsif (/--/ && !/--(.*?);/){ s/--(.*?)$//; print $_,"\n"} elsif (/--(.*?);\s*--(.*?)$/){ s/\s*--$2//; print $_,"\n"} else { print $_,"\n"} } __DATA__ select 'text' from foo; -- comment select '--Not comment' from foo; --But this is select q from z; -- as is this select '--Not this' + '--either' from foo; select 'qaws' + make from "a"; -- comment with 'a' quote select 'a' from 'b' with 'c' -- comment with 'a --' comment -- test comment (add1) select 'text\'s' from foo --escaped ... (add2) select 'text\'s' from foo --escaped' ... (add3) update set bar = bar - 1 ; -- subtraction symbol preserved create table ( -- fo field text, -- fufufu field int)