On Mon, Apr 19, 2010 at 02:56:31PM -0700, Gregg wrote:
> Hi All,
>
> I'm trying to use BBedit to find a string, then extract the previous
> 700 lines everytime it finds it and export this into a new file.
This would work in theory to match the 700 lines:
^((?:.*\r){700})a string
but unfortunately BBEdit runs out of stack space.
You could use it in a Perl script instead, something like this:
#!perl
local $/;
my $file = 'new file';
my $text = <>;
open my $out, '>', $file
or die "Can't open '$file': $!\n";
while ($text =~ /^((?:.*\n){700}).*a string/mg) {
print $out $1;
}
__END__
That works, but regex is really slow. It matches 700 lines, then looks for
'a string' on the next line. If that doesn't match, it backtracks all the
way, moves up one line, matches 700 lines, then looks for 'a string' on the
next line. And so on.
It also doesn't account for 'a string' appearing in the first 700 lines, or
'a string' appearing multiple times within a span of 700 lines.
Here's an alternate approach that is much faster and deals with both those
cases:
#!perl
my $file = 'new file';
open my $out, '>', $file
or die "Can't open '$file': $!\n";
my @lines;
while (<>) {
if (/a string/) {
print $out @lines;
}
push @lines, $_;
shift @lines if @lines > 700;
}
__END__
Of course, you'll want to replace 'new file' and 'a string'. :)
You would have to run this from the command line, specifying the name of
the source file. If you want a script you can run from within BBEdit, I
can work on that when I have a bit more time.
HTH,
Ronald
--
You received this message because you are subscribed to the
"BBEdit Talk" discussion group on Google Groups.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/bbedit?hl=en
If you have a feature request or would like to report a problem,
please email "[email protected]" rather than posting to the group.