About: Sorting lines in a file based on the number of words in each line   Sponge Permalink

An Entity of Type : owl:Thing, within Data Space : 134.155.108.49:8890 associated with source dataset(s)

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) . " " . submatch(0) Explanation, from the inside out: part1 = substitute(submatch(0), "\\S\\+", "x", "g")

AttributesValues
rdfs:label
  • Sorting lines in a file based on the number of words in each line
rdfs:comment
  • 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) . " " . submatch(0) Explanation, from the inside out: part1 = substitute(submatch(0), "\\S\\+", "x", "g")
Version
  • 6(xsd:integer)
dbkwik:vim/property/wikiPageUsesTemplate
Previous
  • 799(xsd:integer)
Complexity
  • intermediate
Created
  • 2004(xsd:integer)
ID
  • 800(xsd:integer)
NEXT
  • 801(xsd:integer)
Rating
  • 4(xsd:integer)
abstract
  • 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) . " " . 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.
Alternative Linked Data Views: ODE     Raw Data in: CXML | CSV | RDF ( N-Triples N3/Turtle JSON XML ) | OData ( Atom JSON ) | Microdata ( JSON HTML) | JSON-LD    About   
This material is Open Knowledge   W3C Semantic Web Technology [RDF Data] Valid XHTML + RDFa
OpenLink Virtuoso version 07.20.3217, on Linux (x86_64-pc-linux-gnu), Standard Edition
Data on this page belongs to its respective rights holders.
Virtuoso Faceted Browser Copyright © 2009-2012 OpenLink Software