Swapping column in vi editor | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Swapping column in vi editor

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-19-2012
muhnandap muhnandap is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 3 October 2013, 3:24 AM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Swapping column in vi editor

is there any command in vi editor to turn this


Code:
986.000   4.026.000   775.328.625   9.319.003.000       
986.000   4.036.000   775.328.625   9.318.803.000       
986.000   4.046.000   775.328.625   9.318.603.000       
986.000   4.056.000   775.328.625   9.318.403.000

become this


Code:
             
4.026.000   986.000   775.328.625   9.319.003.000       
4.036.000   986.000   775.328.625   9.318.803.000       
4.046.000   986.000   775.328.625   9.318.603.000       
4.056.000   986.000   775.328.625   9.318.403.000

Sponsored Links
    #2  
Old 11-19-2012
guruprasadpr's Avatar
guruprasadpr guruprasadpr is offline Forum Advisor  
Shrink...ing
 
Join Date: Jun 2009
Last Activity: 21 October 2014, 6:30 AM EDT
Location: India
Posts: 675
Thanks: 30
Thanked 215 Times in 214 Posts
Hi

Something like this:


Code:
:%s/\([^ ]*\) *\([^ ]*\)/\2 \1 /


Last edited by guruprasadpr; 11-19-2012 at 02:59 AM.. Reason: Removed g to prevent global substitution
Sponsored Links
    #3  
Old 11-19-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts

Code:
:1,4s/\([^ \t]*\)\([ \t]*\)\([^ \t]*\)/\3\2\1/

Change the range 1,4 as per your requirements.
    #4  
Old 11-19-2012
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 21 October 2014, 5:03 PM EDT
Location: Paris
Posts: 4,431
Thanks: 16
Thanked 473 Times in 427 Posts
and in case you find the pure vi solution too complex, you might use awk this way:

Code:
:%!awk '{t=$1;$1=$2;$2=t;print}'


Last edited by jlliagre; 11-19-2012 at 03:16 AM..
Sponsored Links
    #5  
Old 11-19-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
@jlliagre,
I think you missed a 1 or print there.
In any case, the spacing between the columns might get mangled.
Sponsored Links
    #6  
Old 11-19-2012
jlliagre jlliagre is offline Forum Advisor  
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Last Activity: 21 October 2014, 5:03 PM EDT
Location: Paris
Posts: 4,431
Thanks: 16
Thanked 473 Times in 427 Posts
Thanks, I indeed missed a statement, post #4 fixed (I prefer 'print' to the cryptic '1'). Column separators will be normalized to a single space after running this command while your solution preserve the existing separators.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing 2nd Column in Vi Editor muhnandap UNIX for Dummies Questions & Answers 9 09-27-2012 04:27 AM
Delete a specific column using vi editor? fnebiolo UNIX for Dummies Questions & Answers 3 10-25-2010 03:28 AM
Vi editor, copy one column? MeetP Shell Programming and Scripting 3 12-15-2009 01:45 PM
Swapping in VI editor Jahn UNIX for Dummies Questions & Answers 1 06-24-2009 12:18 PM
VI editor,column postion vijay_0209 Shell Programming and Scripting 1 10-29-2008 07:12 AM



All times are GMT -4. The time now is 05:34 PM.