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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.