Wikia

Vim Tips Wiki

Changes: Sorting lines in a file based on the number of words in each line

Edit

Back to page

(fix DrChip changed URL + minor manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 14: Line 14:
 
Here is one use of substitute with an expression ({{help|sub-replace-expression}}).
 
Here is one use of substitute with an expression ({{help|sub-replace-expression}}).
   
I needed to sort a file according to the number of words in each line. Using the <tt>:s</tt> command, I inserted the number of words each line contains, at the beginning of the line.
+
I needed to sort a file according to the number of words in each line. Using the <code>:s</code> command, I inserted the number of words each line contains, at the beginning of the line.
   
 
To simplify sorting (using the excellent [http://www.drchip.org/astronaut/vim/index.html#VISSORT VisSort]), instead of inserting a number, I put the corresponding character (number plus 64, so a line with one word will start with 'A').
 
To simplify sorting (using the excellent [http://www.drchip.org/astronaut/vim/index.html#VISSORT VisSort]), instead of inserting a number, I put the corresponding character (number plus 64, so a line with one word will start with 'A').

Latest revision as of 05:49, July 13, 2012

Tip 800 Printable Monobook Previous Next

created 2004 · complexity intermediate · version 6.0


Here is one use of substitute with an expression (:help sub-replace-expression).

I needed to sort a file according to the number of words in each line. Using the :s command, I inserted the number of words each line contains, at the beginning of the line.

To simplify sorting (using the excellent VisSort), instead of inserting a number, I put the corresponding character (number plus 64, so a line with one word will start with 'A').

Here is the expression I used:

:%s/^.*/\=nr2char(strlen((substitute(substitute(submatch(0), "\\S\\+", "x", "g"), "\\s","","g")))+64) . "\t" . submatch(0)

Explanation, from the inside out:

Replace all consecutive non space by a single x (each word is now a single x):

part1 = substitute(submatch(0), "\\S\\+", "x", "g")

Remove the spaces between "x":

part2 = substitute(part1,"\\s","","g")

Count the number of "x":

part3 = strlen(part2)

Get the ASCII char representing the number of words + 64:

part4 = nr2char(part3+64)

I replace all lines (^.*) by that ASCII char, followed by a tab and the initial line itself.

I can then sort, then remove all characters between the beginning of lines and the first tab.

CommentsEdit

To sort all file:

:sort

To remove prefix of letter and tab:

%s_^.\t__

Around Wikia's network

Random Wiki