Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://886049]
Approved by toolic
[Corion]: LanX: Ah, yeah - Frankfurt is in the quake region, but at the very border. I think I've never noticed a quake in Frankfurt myself
[marto]: good morning all
[Corion]: On another topic, in the process of Rubber Duck SoPWing, I wrote a post about the best API for generating HTTP requests (not sending them) and while writing it and fleshing out my sample implementation, I came up with some improvements to my ...
[Corion]: ... existing prototype. Cartesian Products will be proud of that module once it gets out ;)
[Corion]: Hi marto ;)

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-01-18 10:13 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (161 votes). Check out past polls.