#! perl -w use strict; use warnings; my $outfile = "Outfile.txt"; open (OUT, "+>>$outfile") or die "Cannot append to '$outfile': $!"; FILE: while (<*.sql>) { my $file = $_; next FILE if $file eq 'Activate.sql'; open (my $fh, "<", $file) or die "Cannot read '$file': $!"; print "$file\n"; while (<$fh>) { if (1 == $. and m/ALTER\sPROCEDURE\s/) { next FILE; } if ( not m/INSERT INTO Message_pool/ and m/CREATE TABLE\s([A-Z|a-z|_]{1,200})\s.{0,100}/ ) { print OUT "if exists (select 1 from INFORMATION_SCHEMA.tables where table_name = '$1') DROP TABLE $1\n"; } } }