Well, ++Albannach for not reading the whole file. But you could have saved some typing if we just want to print the first 10 lines:

perl -pe '$.>10&&last' yourfile.txt

However, the original poster might appreciate a little more verbosity and variation (and we'll get the lines into an array instead of just printing them):

# the Albannach method expanded (only read $n_lines from file): #!/usr/bin/perl -w use strict; my $n_lines = 10; my $file = 'blah'; my @lines; open(FILE, $file)|| die "Can't $!"; while(<FILE>){ push @lines, $_; last if $. == $n_lines; } close FILE; print @lines; __END__ # A slight variation (only reads $n_lines): #!/usr/bin/perl -w use strict; my $n_lines = 10; my $file = 'blah'; my @lines; open(FILE, $file)|| die "Can't $!"; for (1 .. $n_lines) { push @lines, scalar <FILE>; } close FILE; print @lines; __END__ # let's read $n_lines one character at a time: #!/usr/bin/perl -w use strict; my $n_lines = 10; my $file = 'blah'; $_ = ''; open(FILE, $file)|| die "Can't $!"; $_ .= getc(FILE) while tr/\n// < $n_lines; close FILE; my @lines = split/^/m; print @lines; __END__ # aww heck, lets read it all but only keep $n_lines: #!/usr/bin/perl -w use strict; my $n_lines = 10; my $file = 'blah'; my @lines; open(FILE, $file)|| die "Can't $!"; @lines[0..$n_lines-1] = <FILE>; close FILE; print @lines; __END__ # same idea, different implementation: #!/usr/bin/perl -w use strict; my $n_lines = 10; my $file = 'blah'; open(FILE, $file)|| die "Can't $!"; my @lines = (<FILE>)[0..$n_lines-1]; close FILE; print @lines; __END__ # stand on someone else's shoulders (or head as the case may be): #!/usr/bin/perl -w use strict; my $n_lines = 10; my $file = 'blah'; my @lines = `head -n$n_lines $file`; print @lines; __END__

so I was bored!


In reply to Re: Re: I feel stupid. by danger
in thread Read n lines from the top of a text file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.