Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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 !!

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-19-2012
Registered User
 
Join Date: Sep 2012
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
Shrink...ing
 
Join Date: Jun 2009
Location: India
Posts: 674
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
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 475 Times in 455 Posts

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

Change the range 1,4 as per your requirements.
    #4  
Old 11-19-2012
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Location: Paris
Posts: 4,135
Thanks: 14
Thanked 397 Times in 365 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
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 475 Times in 455 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
ɹǝsn sıɹɐlos
 
Join Date: Dec 2007
Location: Paris
Posts: 4,135
Thanks: 14
Thanked 397 Times in 365 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
Reply

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 03:44 AM.