In your example, you don't need the $_. This will print lines 1 and 2 from the file test.txt:
If you prefer to be more explicit:perl -ne 'print if 1..2' test.txt
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:
In reply to Re: How does $. work in one liner?
by eyepopslikeamosquito
in thread How does $. work in one liner?
by elevenfly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |