?
Solved

reverse last two columns using perl or shell

Posted on 2011-09-22
6
Medium Priority
?
321 Views
Last Modified: 2012-06-22
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
0
Comment
Question by:libertyforall2
  • 3
  • 2
6 Comments
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 200 total points
ID: 36584932
What have you tried?
0
 

Author Comment

by:libertyforall2
ID: 36584993
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.
0
 
LVL 12

Expert Comment

by:tel2
ID: 36585229
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:tel2
ID: 36585239
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
0
 
LVL 12

Accepted Solution

by:
tel2 earned 1800 total points
ID: 36590939
Alternatively:
    perl -pe 's/(.*) (\S+) (\S+)/$1 $3 $2/' infile.txt >outfile.txt
0
 

Author Closing Comment

by:libertyforall2
ID: 36593052
I used the last one. It works great!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What we learned in Webroot's webinar on multi-vector protection.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question