in reply to perl one liner to print a line

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.

Replies are listed 'Best First'.
Re^2: perl one liner to print a line
by ngungo (Initiate) on Jul 04, 2011 at 17:30 UTC
    How about if I want to print the first line that match a regrex expression. Thanks, ngungo (newbie)