On Thu, 2002-06-27 at 14:46, From Laura wrote:
>
> Hello,
> I am really a beginner not only to perl but to programming! but here my
> question.
>
> I have 300 files that I need to go thru. Each file has an embedded sql
> query. I want to automatically pull out just the lines that represent the
> query text. I cannot make grep do this task. There are several things that
> I know to be true:
>
> 1. The beginning of the range always begins with the word "select"
> 2. The end of the range is always one line above a line that says:
> "-- end of query"
> 3. The lines in between this "sandwich" can vary in number and do not have
> good predictable features or rules.
>
> I know that I will need some programming structure that reads through each
> line of each file and when it finds the occurrence of the word "select" it
> enters into a loop that ends when it finds the string "--end of query." It
> then takes the chunk and outputs to a file.
>
> Like I said, I am very new but any pointers would be helpful. I have been
> told that perl is excellent for such a task and I may be able to get a
> senior programmer here to help me with the loop structure if I can provide
> him with the perl constructs needed. For instance, should I research the
> array function in Perl? Any help would be SO SO appreciated! Thanks!!!
#!/usr/bin/perl
use strict; #always use strict;
my $in_sql = 0;
#read form the standard input, or a list of files
#and put the line in $_
while (<>) {
#set $in_sql to 1 if $_ contains select
#(removing anything before the select)
$in_sql = 1 if s/(.*)(select\s.*)/\2/i;
#set $in_sql to 0 if $_ contains the end token
#at the start of the line
$in_sql = 0 if /^-- end of query/;
#print $_ if we are in SQL
print if $in_sql;
}
or the shorter
#!/usr/bin/perl -n
$in_sql = 1 if s/(.*)(select\s.*)/\2/i;
$in_sql = 0 if /^-- end of query/;
print if $in_sql;
--
Today is Pungenday the 32nd day of Confusion in the YOLD 3168
Wibble.
Missile Address: 33:48:3.521N 84:23:34.786W
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]