How do i skip n number of lines in a file based on a pattern of the record

Discussion in 'Perl Misc' started by sanju.shah@gmail.com, Jul 8, 2005.

  1. Guest

    I am reading each record from an input text file using the foreach
    loop. Once my search of a pattern in a record is true - i want to skip
    next n lines.

    How do i get this done,

    TIA,

    Sanju
     
    , Jul 8, 2005
    #1
    1. Advertising

  2. Re: How do i skip n number of lines in a file based on a patternof the record

    wrote:

    > I am reading each record from an input text file using the foreach
    > loop.


    Really? Why? That's rather unnatural. Can you show us what you mean
    (please see posting guidelines).

    > Once my search of a pattern in a record is true - i want to skip
    > next n lines.
    >
    > How do i get this done,


    In general the way to have a foreach skip is something like...

    my $skip;
    foreach ( whatever ) {
    if ($skip) { $skip--; next }
    if ( some_condition ) {
    $skip = 5;
    }
    }

    But if you took the conventional approach of reading the file a line at
    a time in a while loop is would be simpler. Then you could just read
    and discard some lines.

    local *_;
    while ( <$filehandle> ) {
    if ( some_condition ) {
    <$filehandle> for 1 .. 5;
    }
    }
     
    Brian McCauley, Jul 8, 2005
    #2
    1. Advertising

  3. Paul Lalli Guest

    wrote:
    > I am reading each record from an input text file using the foreach
    > loop. Once my search of a pattern in a record is true - i want to skip
    > next n lines.
    >
    > How do i get this done,


    TIMTOWTDI, but my initial instinct is to use the "flip-flop" operator
    (which is the .. operator used in scalar context). Read more about it
    in
    perldoc perlop

    This program sets the number of lines to skip when a number is found.
    When a number is found, that many lines are skipped. All others are
    printed.

    Paul Lalli

    #!/usr/bin/perl
    use strict;
    use warnings;
    #when find a number, skip the next five lines

    my $skips = 4;
    while (<DATA>){

    next if (/\d/ .. !$skips--);
    print;
    $skips = 4;
    }


    __DATA__
    one
    two
    three
    Here's the 4th!
    five
    six
    seven
    eight
    nine
    ten
    eleven
    Another number: 12
    thirteen
    fourteen
    fifteen
    sixteen
    seventeen
    eightteen
    nineteen
     
    Paul Lalli, Jul 8, 2005
    #3
  4. <> wrote:

    > I am reading each record from an input text file using the foreach
    > loop.



    A foreach reads ALL of the lines into memory at once.

    You should probably be using a while loop instead.


    > Once my search of a pattern in a record is true - i want to skip
    > next n lines.
    >
    > How do i get this done,



    my $n = 5; # untested
    while ( <DATA> ) {
    if ( /some pattern/ ) {
    scalar(<DATA>) for 1 .. $n;
    }
    }


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 8, 2005
    #4
  5. Nico Coetzee Guest

    Re: How do i skip n number of lines in a file based on a patternof the record

    wrote:
    > I am reading each record from an input text file using the foreach
    > loop. Once my search of a pattern in a record is true - i want to skip
    > next n lines.
    >
    > How do i get this done,
    >
    > TIA,
    >
    > Sanju
    >


    As with a previous post, I would rather use a while loop:

    1 #!/usr/bin/perl
    2
    3 use strict;
    4
    5 my $skipcount = 0;
    6 my $skipset = 0;
    7
    8 while(<>){
    9
    10 chomp;
    11 if( $skipset ){
    12
    13 $skipcount++;
    14 if( $skipset == $skipcount ) {
    15
    16 $skipset = 0;
    17 $skipcount = 0;
    18
    19 }
    20
    21 } else {
    22
    23 if( /^skip\s+(\d+)/i ) {
    24
    25 $skipset = $1;
    26
    27 } else {
    28
    29 print "Reading line: $_\n";
    30
    31 }
    32
    33 }
    34
    35 }
    36

    with the file saved as process.pl and the data file called 'file', I get
    the following output:

    $ cat file | ./process.pl
    Reading line: read 1
    Reading line: read 2
    Reading line: read 3
    Reading line: read 4
    Reading line: read 5
    Reading line: read 6
    Reading line: read 7
    Reading line: read 11
    Reading line: read 12
    Reading line: read 15
    Reading line: read 16
    Reading line: read 17
    Reading line: read 18

    $ cat file
    read 1
    read 2
    read 3
    read 4
    read 5
    read 6
    read 7
    skip 3
    read 8
    read 9
    read 10
    read 11
    read 12
    skip 2
    read 13
    read 14
    read 15
    read 16
    read 17
    read 18




    Hope that helps :)

    Cheers

    Nico
     
    Nico Coetzee, Jul 8, 2005
    #5
  6. Nico Coetzee Guest

    Re: How do i skip n number of lines in a file based on a patternof the record

    wrote:
    > I am reading each record from an input text file using the foreach
    > loop. Once my search of a pattern in a record is true - i want to skip
    > next n lines.
    >
    > How do i get this done,
    >
    > TIA,
    >
    > Sanju
    >


    As with a previous post, I would rather use a while loop:

    1 #!/usr/bin/perl
    2
    3 use strict;
    4
    5 my $skipcount = 0;
    6 my $skipset = 0;
    7
    8 while(<>){
    9
    10 chomp;
    11 if( $skipset ){
    12
    13 $skipcount++;
    14 if( $skipset == $skipcount ) {
    15
    16 $skipset = 0;
    17 $skipcount = 0;
    18
    19 }
    20
    21 } else {
    22
    23 if( /^skip\s+(\d+)/i ) {
    24
    25 $skipset = $1;
    26
    27 } else {
    28
    29 print "Reading line: $_\n";
    30
    31 }
    32
    33 }
    34
    35 }
    36

    with the file saved as process.pl and the data file called 'file', I get
    the following output:

    $ cat file | ./process.pl
    Reading line: read 1
    Reading line: read 2
    Reading line: read 3
    Reading line: read 4
    Reading line: read 5
    Reading line: read 6
    Reading line: read 7
    Reading line: read 11
    Reading line: read 12
    Reading line: read 15
    Reading line: read 16
    Reading line: read 17
    Reading line: read 18

    $ cat file
    read 1
    read 2
    read 3
    read 4
    read 5
    read 6
    read 7
    skip 3
    read 8
    read 9
    read 10
    read 11
    read 12
    skip 2
    read 13
    read 14
    read 15
    read 16
    read 17
    read 18




    Hope that helps :)

    Cheers

    Nico
     
    Nico Coetzee, Jul 8, 2005
    #6
    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. FrancisC
    Replies:
    7
    Views:
    471
    Howard
    Oct 10, 2003
  2. FrancisC
    Replies:
    16
    Views:
    748
  3. puzzlecracker

    skip blank lines

    puzzlecracker, Oct 16, 2005, in forum: C++
    Replies:
    3
    Views:
    586
    Jim Langston
    Oct 17, 2005
  4. scad
    Replies:
    4
    Views:
    958
    James Kanze
    May 28, 2009
  5. Jie
    Replies:
    6
    Views:
    161
    Peter Makholm
    Jun 28, 2007
Loading...

Share This Page