Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort rows in 'VI' editor

Tags:

vi

text-editor

If i have to sort following rows on the basis of values on left of '='. But the sorting should expand the selection to column after '=' simultaneously. Thtz is we dnt have to sort column after '=' ::

50599=1000000  
50454=00000054  
50080=00005464  
50098=00000875  
50661=00000665  
50788=10000035  
50988=10000006  
50994=10000656  
57009=00000005  
57022=10000008  
57040=10000005  
57000=10000005  
57060=10000089  
57067=10005640  
57102=00000765  
57190=00000867  

This needs to be done in 'VI' editing the file.

RESULT should be ::

50080=00005464    
50098=00000875  ...etc.
like image 233
ErAB Avatar asked Oct 05 '10 07:10

ErAB


People also ask

How do I sort a row in Linux?

To sort lines of text files, we use the sort command in the Linux system. The sort command is used to prints the lines of its input or concatenation of all files listed in its argument list in sorted order. The operation of sorting is done based on one or more sort keys extracted from each line of input.

How do I rearrange lines in vim?

In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up. After visually selecting a block of lines (for example, by pressing V then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.

How do I select all lines in vi mode?

If you want to select the entire line in a file, press V. Now when you press k or j to go up and down, vim will select the entire line above and below your cursor. Finally, you can select text in columns by pressing ctrl+v and moving up or down the block.


1 Answers

Try:

:%!sort

It will sort according the whole line alphabetically. If you want to sort numerically (i.e. the number in the first column can have different widt), then try:

:%!sort -n

Don't worry about the =, it will not modify any line, it will just change their order.

like image 115
eumiro Avatar answered Sep 21 '22 10:09

eumiro