Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How does $. work in one liner?

by eyepopslikeamosquito (Bishop)
on May 29, 2016 at 04:08 UTC ( #1164428=note: print w/replies, xml ) Need Help??


in reply to How does $. work in one liner?

In your example, you don't need the $_. This will print lines 1 and 2 from the file test.txt:

perl -ne 'print if 1..2' test.txt
If you prefer to be more explicit:
perl -ne 'print if $. >= 1 && $. <= 2' test.txt

This type of question comes up often enough that I keep a list of one-liner references.

Update: See also:

Replies are listed 'Best First'.
Re^2: How does $. work in one liner?
by afoken (Canon) on May 29, 2016 at 08:51 UTC
    In your example, you don't need the $_. This will print lines 1 and 2 from the file test.txt:

    Assuming an OS that follows the POSIX spirit, this will also print the first two lines of a file, but probably without reading the entire file, and probably without starting an interpreter:

    head -n 2 test.txt

    (POSIX: head)

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164428]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2021-10-15 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (68 votes). Check out past polls.

    Notices?