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

S

sanju.shah

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
 
B

Brian McCauley

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;
}
}
 
P

Paul Lalli

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
 
T

Tad McClellan

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;
}
}
 
N

Nico Coetzee

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
 
N

Nico Coetzee

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top