sugar has asked for the wisdom of the Perl Monks concerning the following question:

dear monks, how do i open a file and print a specific line using perl one liner. file.txt contains:
there is more than one way to do it
I want to print only the 4th line "one way to". i tried something like
perl -i -e 'while(<>){print $_[3];}' file.txt
can u please correct me if i m wrong !! thanks :)

Replies are listed 'Best First'.
Re: perl one liner to print a line
by jwkrahn (Abbot) on Dec 12, 2008 at 01:43 UTC
      You can exit sooner with
      perl -ne'print, last if $. == 4'

        I prefer:

        perl -ne'print and close ARGV if $. == 4' file.txt

        It works better with multiple files on the command line.      ;-)

Re: perl one liner to print a line
by ikegami (Patriarch) on Dec 12, 2008 at 01:58 UTC
    The array approach would be
    perl -e'@x=<>; print $x[3]'

    but that loads the entire file into memory where the $. method doesn't. Just putting it out there since you tried to use an array.

      yea... i dont want to take the whole file into memory. so $. is a better way for me :)

      Or:

      perl -F'\n' -lan0777e'print $F[3]' file.txt
Re: perl one liner to print a line
by oko1 (Deacon) on Dec 12, 2008 at 02:37 UTC

    And TMTOWTDI again:

    perl -wne'print if 4..4' file.txt

    --
    "Language shapes the way we think, and determines what we can think about."
    -- B. L. Whorf
Re: perl one liner to print a line
by kyle (Abbot) on Dec 12, 2008 at 02:28 UTC

    TIMTOWTDI...

    perl -e '<> for 1..3; print scalar <>' file.txt
Re: perl one liner to print a line
by Lawliet (Curate) on Dec 12, 2008 at 02:55 UTC

    TMTOWTDI meets Occam's razor :P

    perl -e 'print "one way to"'

    I'm so adjective, I verb nouns!

    chomp; # nom nom nom

      :D
Re: perl one liner to print a line
by mr_mischief (Monsignor) on Dec 12, 2008 at 04:08 UTC
    YAWTDI, this time with no whitespace in the source:

    perl -e '{$_=<>;$.<4?redo:print}'

    That really does look kinda like line noise. I wouldn't recommend that for serious work, of course.

      4 chars shorter:

      perl -ne'print,last,if$.==4' 12345678901234567890123 23

      Update: 5 chars shorter:

      perl -nlE'say,last,if$.==4' 1234567890123456789012 22
        I can save another character if we reintroduce whitespace:
        perl -ne'$.-4or print,last'
        or almost identically:
        perl -ne'$.<4or print,last'

        Update:

        perl -nlE'$.<4or say,last' 123456789 123456789 1 21
Re: perl one liner to print a line
by telemachus (Friar) on Dec 12, 2008 at 13:14 UTC
    Lots of people have suggested good ways to do it, but here are a few points about your original version.

    perl -i -e 'while(<>){print $_[3];}' file.txt

    The -i switch says that you want to edit in place. Normally you use that like this: -i.bak. The .bak (or whatever else) becomes a suffix for a copy of your original file. The original becomes file.txt.bak and file.txt gets edited. If you use -i by itself without a suffix, then you don't get a backup. That's dangerous. Since you're not editing at all here, you don't want the switch. (In this example, if you had successfully done print if $. == 4 and used the -i switch with no suffix, you would get no backup and only line 4 left in your original file. I don't think that's what you wanted.)

    The -p and -n switches are worth looking at in perlrun since they will give you the while(<>) loop for free.

      How about if I want to print the first line that match a regrex expression. Thanks, ngungo (newbie)
Re: perl one liner to print a line (GOLF!)
by ccn (Vicar) on Dec 12, 2008 at 07:14 UTC

      die"$_\n" sends to STDERR, so you need to add " 2>&1".

      The second doesn't do the same thing as the other golfs so far. It reads the entire file whereas the other golfs spent extra character to stop after 4 lines. If reading the entire file is ok, then I put forth

      perl -lnE"$.^4||say" 123456789012345 15