Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Reorganizing Columns with Perl One Liner

by neversaint (Deacon)
on Nov 25, 2011 at 14:00 UTC ( [id://940072]=perlquestion: print w/replies, xml ) Need Help??

neversaint has asked for the wisdom of the Perl Monks concerning the following question:

Dear Masters,
How do you reorganize this with one liner
foo r1.1 abc foo r10.1 pqr qux r2.1 lmn bar r33.1 xpq # In fact there could be more fields that preceeds column with "rxx.x" +.
Into this
r1.1 foo abc r10.1 foo pqr r2.1 qux lmn r33.1 bar xpq
Basically, put second column into the first and everything else that preceeds it, after.

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Reorganizing Columns with Perl One Liner
by choroba (Cardinal) on Nov 25, 2011 at 14:14 UTC
    Something like this?
    perl -pe 's/(.*?)\s+(r[0-9]+\.[0-9]+)/$2 $1/'
    It breaks the nice table-like justification, though.

    Update: It is considered polite to inform us you crossposted the question: StackOverflow.

Re: Reorganizing Columns with Perl One Liner
by BrowserUk (Patriarch) on Nov 25, 2011 at 14:54 UTC

    C:\test>copy con junk.txt foo r1.1 abc foo r10.1 pqr qux r2.1 lmn bar r33.1 xpq ^Z 1 file(s) copied. C:\test>perl -anle"print join chr(9), @F[ 1,0,2 ]" junk.txt r1.1 foo abc r10.1 foo pqr r2.1 qux lmn r33.1 bar xpq

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Reorganizing Columns with Perl One Liner
by johngg (Canon) on Nov 25, 2011 at 14:18 UTC

    Something along these lines perhaps?

    knoppix@Microknoppix:~$ perl -Mstrict -wE ' > open my $inFH, q{<}, \ <<EOD or die $!; > foo r1.1 abc > foo r10.1 pqr > qux r2.1 lmn > bar r33.1 xpq > EOD > > printf qq{%-6s%-4s%-s\n}, ( split )[ 1, 0, 2 ] > for <$inFH>;' r1.1 foo abc r10.1 foo pqr r2.1 qux lmn r33.1 bar xpq knoppix@Microknoppix:~$

    You would need to tweak this a bit for extra fields and might have to take the approach of generating the printf format progammatically.

    Cheers,

    JohnGG

Re: Reorganizing Columns with Perl One Liner
by NetWallah (Canon) on Nov 25, 2011 at 15:24 UTC
    perl -ane 'print join ("\t",splice(@F, 1 ,1),@F ), "\n"' YourFileNam +e.txt
    Change the middle number in the 'splice' to select an alternative column (minus one, if you think one-relative).

    If you wan to parameterize, and make the selected column one-relative, you could do this:

    perl -sane 'print join ("\t",splice(@F, $COL-1 ,1),@F ), "\n"' -- -CO +L=2 YourFileName.txt
    Note that you need the "--" to separate 'perl' options from program options.

    or - this option that uses fewer keystrokes and space-separated columns:

    perl -sape '$_=splice(@F, $COL-1,1)." @F\n"' -- -COL=3 YourFileName.tx +t

                "XML is like violence: if it doesn't solve your problem, use more."

Re: Reorganizing Columns with Perl One Liner
by Util (Priest) on Nov 25, 2011 at 23:49 UTC
    perl -pale '$_ = "@F[1,0,2..$#F]"'
Re: Reorganizing Columns with Perl One Liner
by pvaldes (Chaplain) on Nov 25, 2011 at 14:23 UTC

    You could swap both cols in a while or for loop, for instance

    foreach (@col){ ($col[0], $col[1]) = ($col[1], $col[0]);}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://940072]
Approved by muba
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found