Re: How good is PERL at searching ASCII files?
- From: "James" <hslee911@xxxxxxxxx>
- Date: 8 Dec 2006 11:58:41 -0800
jmartzoo-google@xxxxxxxxx wrote:
Hi,
Please forgive my ignorance, I'm a C language developer but I've got
some text manipulation that needs doing and I've heard many times that
this is PERL's forte. That being said, I'd like to give it a try
instead of writing a C# app to deal with this. Can PERL do th
following relatively easy, or should I just spend 3 or 4 hours writing
a C# algorithm?
The challenge I'm faced with is to search out all occurrences where a
function named CONVERT() is being called with three parameters. It's a
challenge because the function has several signatures and I need to
segregate the three param calls from the two param calls. To make
things even spicier, it's very possible that the parameters may also be
function calls themselves. I think the best strategy becomes finding
the pattern:
CONVERT(<something>,<something>,
The key elements being the string CONVERT, one open paren ( followed by
two commas at the same nesting level.
Of the following examples, I would want the first two in my results,
the second two ignored:
yes:
~~~~
1)
CONVERT(varchar(16), @variable, 1)
2)
convert (char(12),
(
SELECT date
FROM table1
WHERE id = @x
AND user IN ('raquel', 'marylin', 'jennifer')
), 7)
no:
~~~~
1)
convert(varchar(16), @variable)
2)
CONVERT (char(12),
(
SELECT date
FROM table2
WHERE id = @y
AND user IN ('huey', 'dewey', 'louie')
))
If anybody has a script that I can use to get started, I'd be very
grateful to see it. I've got 1,500 flat files (ascii) that need
parsing and a search on the string convert hits 10,000 times... my
guess is that only 5% of those will be the three parameter version I
need to isolate.
Much obliged,
John M
Montreal, Quebec
#!/bin/env perl
$/="";
while (<>) {
$txt = $_;
$par = $com = 0;
next unless /^\s*convert\s*\(/i;
for (split //) {
$par++ if /\(/;
$par-- if /\)/;
$com++ if $par==1 && /\,/;
}
print $txt if $com==2;
}
JL
.
- References:
- How good is PERL at searching ASCII files?
- From: jmartzoo-google
- How good is PERL at searching ASCII files?
- Prev by Date: Re: How good is PERL at searching ASCII files?
- Next by Date: Re: why does this script not work? Mystery solved.
- Previous by thread: Re: How good is PERL at searching ASCII files?
- Next by thread: Re: How good is PERL at searching ASCII files?
- Index(es):