Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

perl one liner print columns 2.. last

by Anonymous Monk
on Jan 04, 2013 at 18:48 UTC ( #1011694=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

can someone help? I'm trying to create a one liner that will split a file into columns and print the 2nd through the last column. sort of like this (doesn't work) Appreciate any help

cat /tmp/1.out|perl -lane 'print $F[1]..$F[-1]'

Comment on perl one liner print columns 2.. last
Download Code
Re: perl one liner print columns 2.. last
by philipbailey (Chaplain) on Jan 04, 2013 at 19:03 UTC

    Works for me. I would suggest that you show us (an extract) of your input file.

    echo 1 2 3 4 5 6|perl -lane 'print $F[1]..$F[-1]' 23456

    Are your columns separated by whitespace?

    Update: Of course, this doesn't work, for the reasons given by LanX in his reply to this (too hasty) posting.

      Sorry thats wrong, your test data is misleading you :)

      you are calculating the numerical range between 2 and 6, which by accident was your input.

      now change the input in between:

      > echo - 2 - - - 6|perl -lane 'print $F[1]..$F[-1]' 23456

      but a slice works: =)

      > echo - 2 - - - 6|perl -lane 'print @F[1..$#F]' 2---6

      Cheers Rolf

Re: perl one liner print columns 2.. last
by dbuckhal (Monk) on Jan 04, 2013 at 19:06 UTC
    I got this from an example in "Effective Perl Programming", pg 429. This will skip the first line of a file, but what type of columns are you trying to generate?
    perl -ne "print unless $. == 1;" filename(s)
Re: perl one liner print columns 2.. last
by aguilarsoto (Novice) on Jan 04, 2013 at 19:10 UTC
    something like this may work too
    cat /tmp/1.out |perl -lane 'shift @F; print @F'
Re: perl one liner print columns 2.. last
by mhearse (Hermit) on Jan 04, 2013 at 19:37 UTC
    awk does such things well. Uses \s+ delimiter by default.
    cat file | awk '{ print ( $2, $(NF-1) ) }'
Re: perl one liner print columns 2.. last
by Jim (Curate) on Jan 04, 2013 at 22:03 UTC

    Consider using cut instead of Perl.

    cut -f 2- /tmp/1.out

    Jim

Re: perl one liner print columns 2.. last
by eyepopslikeamosquito (Canon) on Jan 04, 2013 at 23:34 UTC

    Long ago on usenet, Tom Christiansen once wrote: "If you find yourself calling cat with just one argument, you're probably doing something silly". So you should lose the cat and replace:

    cat /tmp/1.out|perl -lane 'print $F[1]..$F[-1]'
    with (using the LanX slice idea):
    perl -lane 'print join q{ }, @F[1..$#F]' /tmp/1.out
    or:
    perl -lape '$_=join q{ }, @F[1..$#F]' /tmp/1.out
    or even (for variety, not recommended):
    perl -lpe 's/\s*\S+\s*//' /tmp/1.out

      Also long ago on Usenet, there was the Useless Use of cat award, usually awarded to a practitioner of "cat abuse" by merlyn. I won one once!

      Jim

Re: perl one liner print columns 2.. last
by Anonymous Monk on Jan 05, 2013 at 14:22 UTC

    Thanks for all the timely and helpful replies! Even answers not actually solving the problem, are very educational. I should have provided more detail, this problem has stymied me many times in many situations, and I finally got frustrated enough to ask the question. (and got the answer(s)!) In this case I wanted to print history without the line numbers on lines that were whitespace separated and variable columns. Thanks to all again! These worked for me ...

    history|perl -lane 'print join q{ }, @F[1..$#F]' perl -lane 'print join q{ }, @F[1..$#F]' /tmp/1.out

      If you have both history and Perl, then you probably also have cut.

      history | cut -f 2-

      On my system, -n suppresses line numbers (but not the leading tab).

      history -n

      Though it's handy to know -a and @F autosplit command-line idioms for use in some situations, this specific situation is one for which there's a simpler, less arcane way that doesn't require Perl. Also, using cut is less destructive than using Perl because it doesn't convert tabs to spaces as the Perl autosplit trick does. (Of course, this can be remedied by specifying an alternate separator pattern with the -F command-line option, but this is just leading you down the garden path.)

      Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011694]
Approved by LanX
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-07-29 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls