Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

using command line autosplit option

by umasuresh (Hermit)
on Feb 03, 2011 at 19:34 UTC ( #886049=perlquestion: print w/replies, xml ) Need Help??
umasuresh has asked for the wisdom of the Perl Monks concerning the following question:

Oh enlightened monks!
I am trying to use a on liner to extract the first three columns of file1 using the following command
perl -pane 'print "$F[0]\t$F[1]\t$F[2]";' file1.txt
But it prints the whole line along with the first 3 columns.
prints: chr1 58963 Gchr1 58963 G G 10 0 10 + 1 ^+. 8 __file1__ chr1 58963 G G 10 0 10 1 ^+. + 8
How do I fix this? Thanks much!

Replies are listed 'Best First'.
Re: using command line autosplit option
by Fletch (Chancellor) on Feb 03, 2011 at 19:36 UTC

    The -p and -n flags are mutually exclusive (you want the later in this case). See perlrun.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: using command line autosplit option
by toolic (Bishop) on Feb 03, 2011 at 19:53 UTC
    Another way, with fewer characters, using perl 5.10 (-E and say), -l and array slices:
    perl -lanE'say join"\t",@F[0..2]' file.txt
      Thanks much Toolic and Fletch!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://886049]
Approved by toolic
help
Chatterbox?
and the shadows deepen...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2017-09-21 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (244 votes). Check out past polls.

    Notices?