sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #800: Sorting lines in a file based on the number of words in each line

 tip karma   Rating 0/0, Viewed by 286 

created:   October 4, 2004 18:23      complexity:   intermediate
author:   Anonymous      as of Vim:   6.0

Here is one use of the 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, at the begining of each line, I
insert the number of words it contains.

To simplify sorting (using the excellent VisSort, see
http://mysite.verizon.net/astronaut/vim/), instead of inserting a number, I put
the corresponding character (plus 64, to start with A).

Here is the expression I use:

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

Now decomposed in parts, from the inside out:
part 1 = substitute(submatch(0), "\\S\\+", "x", "g")
  replace all consecutive non space by a single x (each word is now a single x)

part 2 = (substitute(part 1),"\\s","","g")
  remove the spaces between "x"
  
part 3 =  (strlen(part 2)
  count the number of "x"

part 4 = nr2char(part 3)+64
  get the ASCII char representing the number of words + 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 begining of lines and
the first tab.
                            

 rate this tip  Life Changing Helpful Unfulfilling 

<<Searching for files | Folding of (gnu)make output >>

Additional Notes

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo