Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort numerically in VI editor [duplicate]

Possible Duplicate:
How to sort numeric and literal columns in Vim

I have to sort the below lines based on thread id.

Internal thread 0 bound to OS proc set {1} Internal thread 1 bound to OS proc set {5} Internal thread 2 bound to OS proc set {9} Internal thread 10 bound to OS proc set {41} Internal thread 9 bound to OS proc set {37} 

When I issue :!sort -n they get sorted like this:

Internal thread 0 bound to OS proc set {1} Internal thread 1 bound to OS proc set {5} Internal thread 10 bound to OS proc set {41} Internal thread 2 bound to OS proc set {9} Internal thread 9 bound to OS proc set {37} 

But I need them to be sorted like this:

Internal thread 0 bound to OS proc set {1} Internal thread 1 bound to OS proc set {5} Internal thread 2 bound to OS proc set {9} Internal thread 9 bound to OS proc set {37} Internal thread 10 bound to OS proc set {41} 
like image 854
arunmoezhi Avatar asked Dec 28 '12 21:12

arunmoezhi


People also ask

How do I sort a specific column in vim?

column happens to be the first column with a decimal number, note that you could use simply :sort n , as the help says: "With [n] ... sorting is done on the first decimal number in the line..." This doesn't apply to your case, but might to somebody elses.


1 Answers

Just use Vim's own sort function. Visually highlight the text (or use a range) and type:

:sort n

Documentation is available here: http://vim.wikia.com/wiki/Sort_lines

Or in Vim itself: :help sort

(Edited to reflect an important point of clarification from dash-tom-bang and the reference to Vim's own help file.)

like image 79
Nate Avatar answered Sep 21 '22 05:09

Nate