A question about regex

A question about regex

am 19.08.2007 16:18:15 von Madhusudhanan Chandrasekaran

Hi,

I am a perl newbie. I am reading from a file line by line and
matching it for a partiuclar regex. If the match is found, I want
to print the previous and the next few lines.

i.e. if the lines of the file are:

This is a
String
But I do not
know how to
print it


and if my pattern is "But I do not", I would like to print as
"String But I do not know how to". Here it prints out the previous
and the next line, keeping it variable is desired.

Thanks in advance,
_Madhu

--------------------------------------------------
All in all I'm just another brick in the FIREWALL.

Re: A question about regex

am 19.08.2007 16:36:37 von Paul Lalli

On Aug 19, 10:18 am, Madhusudhanan Chandrasekaran
wrote:
> I am a perl newbie. I am reading from a file line by line and
> matching it for a partiuclar regex. If the match is found, I want
> to print the previous and the next few lines.
>
> i.e. if the lines of the file are:
>
> This is a
> String
> But I do not
> know how to
> print it
>
> and if my pattern is "But I do not", I would like to print as
> "String But I do not know how to".

Show what you've done so far, and someone can help you out.

Also, have you checked the FAQ?
$ perldoc -q "more than one line"
Found in /software/perl-5.8.5-0/pkg/lib/5.8.5/pod/perlfaq6.pod
I'm having trouble matching over more than one line. What's
wrong?

In general, if you're processing line-by-line like you say you are,
there is no particular regexp that's going to help you. As you read
through each line, save the current line in some variable, then when
you find your match, print that variable you saved, print the current
line, and read and print once more.

Paul Lalli

Re: A question about regex

am 19.08.2007 17:12:44 von jurgenex

Madhusudhanan Chandrasekaran wrote:
> I am a perl newbie. I am reading from a file line by line and
> matching it for a partiuclar regex. If the match is found, I want
> to print the previous and the next few lines.

Nothing to do with Perl or REs but rather with basic algorithm design.
Because you are reading the file line by line and you can only detect the
desired line _after_ working on the next one already you need to keep a
history of one line, something like $previous_line which you update every
single time you read a new line.

Adding the following lines, too, is not a big deal. Just read and print them
when your condition is true.

jue

Re: A question about regex

am 19.08.2007 19:30:00 von Tad McClellan

Madhusudhanan Chandrasekaran wrote:


> I am a perl newbie. I am reading from a file line by line and
> matching it for a partiuclar regex. If the match is found, I want
> to print the previous and the next few lines.
>
> i.e. if the lines of the file are:
>
> This is a
> String
> But I do not
> know how to
> print it
>
>
> and if my pattern is "But I do not", I would like to print as
> "String But I do not know how to". Here it prints out the previous
> and the next line, keeping it variable is desired.


--------------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Tie::File;

tie my @array, 'Tie::File', 'file' or die "could not tie 'file' $!";
my $prev = 1;
my $next = 1;

foreach my $i ( 0 .. $#array ) {
next unless $array[$i] =~ /^But I do not$/;
print "@array[ $i-$prev .. $i+$next ]\n";
}
--------------------------------


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: A question about regex

am 19.08.2007 23:48:23 von Henry Law

Madhusudhanan Chandrasekaran wrote:
> Hi,
>
> I am a perl newbie. I am reading from a file line by line and
> matching it for a partiuclar regex. If the match is found, I want
> to print the previous and the next few lines.

I'm all for using Perl, but if you're on a UNIX system then grep -A1 -a1
would do this. (Might even work on another OS with a grep port).

--

Henry Law Manchester, England

Re: A question about regex

am 20.08.2007 14:15:51 von anno4000

Tad McClellan wrote in comp.lang.perl.misc:
> Madhusudhanan Chandrasekaran wrote:
>
>
> > I am a perl newbie. I am reading from a file line by line and
> > matching it for a partiuclar regex. If the match is found, I want
> > to print the previous and the next few lines.
> >
> > i.e. if the lines of the file are:
> >
> > This is a
> > String
> > But I do not
> > know how to
> > print it
> >
> >
> > and if my pattern is "But I do not", I would like to print as
> > "String But I do not know how to". Here it prints out the previous
> > and the next line, keeping it variable is desired.
>
>
> --------------------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Tie::File;
>
> tie my @array, 'Tie::File', 'file' or die "could not tie 'file' $!";
> my $prev = 1;
> my $next = 1;
>
> foreach my $i ( 0 .. $#array ) {
> next unless $array[$i] =~ /^But I do not$/;
> print "@array[ $i-$prev .. $i+$next ]\n";
> }

Ah, that's a good use of Tie::File. It hides the complexities of treating
a file as an array of lines (which you want to do for that problem) without
worrying about slurping the entire file.

It is still not perfect. When the next line is "But I do not" again,
it starts another triplet that partially overlaps with the one already
printed.

There are also no provisions in case the very first (or last) line is
a match, in which cases $i-$prev or $i+$next may leave the range of
defined lines.

One solution would be to collect indices of lines to print in an array
(@print_these) and use a hash (%seen) to detect duplicates. After the
loop, negative indices can be shifted off @print_these, and indices
beyond $#array can be popped. Finally, @array[ @print_these] can be
output.

Anno

Re: A question about regex

am 20.08.2007 17:59:54 von glex_no-spam

Henry Law wrote:
> Madhusudhanan Chandrasekaran wrote:
>> Hi,
>>
>> I am a perl newbie. I am reading from a file line by line and
>> matching it for a partiuclar regex. If the match is found, I want
>> to print the previous and the next few lines.
>
> I'm all for using Perl, but if you're on a UNIX system then grep -A1 -a1

grep -A 1 -B 1

Re: A question about regex

am 20.08.2007 22:26:43 von rvtol+news

J. Gleixner schreef:
> Henry Law:
>> Madhusudhanan Chandrasekaran:

>>> I am a perl newbie. I am reading from a file line by line and
>>> matching it for a partiuclar regex. If the match is found, I want
>>> to print the previous and the next few lines.
>>
>> I'm all for using Perl, but if you're on a UNIX system then grep
>> -A1 -a1
>
> grep -A 1 -B 1

I also like grep's -P a lot.
:)

--
Affijn, Ruud

"Gewoon is een tijger."