perl script to search for sql statement from a file with keywords -
i trying search in .sql file sql statement starts create table followed fields values keywords [tb_data , tb_indx] , ends ;
like
create table hdtb_code (idpk varchar(256) not null) in tb_data index in tb_indx; .sql file statement in multiple lines
create table hdtb_code ( idpk varchar(256) not null ) in tb_data index in tb_indx; --create table hdtb_byte ( rtid varchar(256) not null ) in tb_data index in tb_indx; drop table hdtb_byte; create table hdtb_res ( artid varchar(256) not null ) in tb_data index in tb_indx; create table hdtb_de ( idpk varchar(256) not null ); -------------output---------------------- create table hdtb_code ( idpk varchar(256) not null ) in tb_data index in tb_indx; create table hdtb_res ( artid varchar(256) not null ) in tb_data index in tb_indx;
try this:
use strict; use warnings; $filename = 'somefile.sql'; open(my $fh, '<', $filename) || die "unable open '$filename' read; $!"; local $/ = ""; # paragraph mode while (<$fh>) { if (/^create table.+tb_data.+tb_indx/s) { print # or else } } close $fh; the use of empty string in $/ marks "paragraph mode", means record separator 2 (or more) new-lines, breaks on blank lines (if not case, use ";\n").
the s modifier after regular expression required . match new-lines.
Comments
Post a Comment