Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Skip line N while reading a file

Tags:

loops

perl

How would I skip say line 9 while I'm parsing a text file?

Here's what I got

use strict; use warnings;
open(my $fh, '<', 'file.txt') or die $!;
my $skip = 1;
while (<$fh>){
    $_=~ s/\r//;
    chomp;
    next if ($skip eq 9)
    $skip++;
}

Not sure if this works but I'm sure there's a more eloquent of doing it.

like image 246
cooldood3490 Avatar asked Dec 12 '22 16:12

cooldood3490


1 Answers

You can use $.:

use strict; use warnings;
open(my $fh, '<', 'file.txt') or die $!;
while (<$fh>){
    next if $. == 9;
    $_=~ s/\r//;
    chomp;
    # process line
}

Can also use $fh->input_line_number()

like image 132
imran Avatar answered Dec 30 '22 06:12

imran