Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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!

Comment on using command line autosplit option
Select or Download Code
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 web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2015-07-02 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (27 votes), past polls