#!/usr/bin/perl $/ = undef; $_ = <>; s/([^#"']*(['][^']*['])|(["][^"]*["]))|([#][^\n]*)/defined $1 ? $1 : ""/gse; print; #### # comment. "this is a quote" # end of line comment. print "this is a multiline quite. #right? "; No comments at all. This is just general stuff. #this is a 'test too." ' #### "this is a quote" print "this is a multiline quote. # right? "; No comments at all. This is just general stuff.