A question about regex

Discussion in 'Perl Misc' started by Madhusudhanan Chandrasekaran, Aug 19, 2007.

  1. 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.
    Madhusudhanan Chandrasekaran, Aug 19, 2007
    #1
    1. Advertising

  2. Madhusudhanan Chandrasekaran

    Paul Lalli Guest

    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
    Paul Lalli, Aug 19, 2007
    #2
    1. Advertising

  3. 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
    Jürgen Exner, Aug 19, 2007
    #3
  4. 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/"
    Tad McClellan, Aug 19, 2007
    #4
  5. Madhusudhanan Chandrasekaran

    Henry Law Guest

    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
    Henry Law, Aug 19, 2007
    #5
  6. Madhusudhanan Chandrasekaran

    -berlin.de Guest

    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
    -berlin.de, Aug 20, 2007
    #6
  7. Madhusudhanan Chandrasekaran

    J. Gleixner Guest

    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
    J. Gleixner, Aug 20, 2007
    #7
  8. Madhusudhanan Chandrasekaran

    Dr.Ruud Guest

    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."
    Dr.Ruud, Aug 20, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    683
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,598
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    585
  4. Xah Lee
    Replies:
    1
    Views:
    924
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    716
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page