Link to home
Start Free TrialLog in
Avatar of libertyforall2
libertyforall2Flag for United States of America

asked on

reverse last two columns using perl or shell

I have the attached file. I want to do 2 things. 1. reverse the last two columns & 3 round all values in the last two columns to the nearest hundredth. A sample file is attached.
so4pap.txt
SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of libertyforall2

ASKER

I've tried importing it into excel then saving as a text file after I switch columns. It leaves gaps between columns for certain rows and does work correctly. I simply want to switch the last two columns. uhggg.
Hi lfa2,

To put output in a 2nd file:
    perl -lane 'print "$F[0] $F[1] $F[3] $F[2]"' infile.txt >outfile.txt

To modify the input file:
    perl -i -lane 'print "$F[0] $F[1] $F[3] $F[2]"' infile.txt
Or more concisely:
    perl -lane 'print "@F[0,1,3,2]"' infile.txt >outfile.txt
or:
    perl -i -lane 'print "@F[0,1,3,2]"' infile.txt
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I used the last one. It works great!