Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How do i skip n number of lines in a file based on a pattern of the record

Reply
Thread Tools

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

 
 
sanju.shah@gmail.com
Guest
Posts: n/a
 
      07-08-2005
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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      07-08-2005


http://www.velocityreviews.com/forums/(E-Mail Removed) 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;
}
}

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-08-2005
(E-Mail Removed) 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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-08-2005
(E-Mail Removed) <(E-Mail Removed)> 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
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Nico Coetzee
Guest
Posts: n/a
 
      07-08-2005
(E-Mail Removed) 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
 
Reply With Quote
 
Nico Coetzee
Guest
Posts: n/a
 
      07-08-2005
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
skip first N lines when reading file Jie Perl Misc 6 06-28-2007 09:07 AM
skip blank lines puzzlecracker C++ 3 10-17-2005 05:33 AM
How can I open a file, skip first 2 lines and get the 50th character? FrancisC C Programming 16 10-14-2003 12:34 PM
How can I open a file, skip first 2 lines and get the 50th character? FrancisC C++ 7 10-10-2003 07:11 PM



Advertisments