About: Sort lines by a specified word number   Sponge Permalink

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

Based on the script by Robert Webb found at [help eval-examples] to sort lines, I made some modifications to enable Sort() to sort lines according to word number count. I defined words to be clusters of non-whitespace characters separated by whitespace characters. For example, the line "while (k == 0)" has 4 words. To use, get into visual-block mode (hit Ctrl-V from normal mode, or Ctrl-Q in Windows) and highlight the lines you wish to sort. Then hit or other key mapping of your choice. You will be prompted to enter a number, which is the word number count from the left. Example:

AttributesValues
rdfs:label
  • Sort lines by a specified word number
rdfs:comment
  • Based on the script by Robert Webb found at [help eval-examples] to sort lines, I made some modifications to enable Sort() to sort lines according to word number count. I defined words to be clusters of non-whitespace characters separated by whitespace characters. For example, the line "while (k == 0)" has 4 words. To use, get into visual-block mode (hit Ctrl-V from normal mode, or Ctrl-Q in Windows) and highlight the lines you wish to sort. Then hit or other key mapping of your choice. You will be prompted to enter a number, which is the word number count from the left. Example:
Version
  • 6(xsd:integer)
dbkwik:vim/property/wikiPageUsesTemplate
Previous
  • 922(xsd:integer)
Author
  • Gerald Lai
Complexity
  • intermediate
Created
  • 2005(xsd:integer)
ID
  • 923(xsd:integer)
NEXT
  • 925(xsd:integer)
Rating
  • 5(xsd:integer)
abstract
  • Based on the script by Robert Webb found at [help eval-examples] to sort lines, I made some modifications to enable Sort() to sort lines according to word number count. I defined words to be clusters of non-whitespace characters separated by whitespace characters. For example, the line "while (k == 0)" has 4 words. To use, get into visual-block mode (hit Ctrl-V from normal mode, or Ctrl-Q in Windows) and highlight the lines you wish to sort. Then hit or other key mapping of your choice. You will be prompted to enter a number, which is the word number count from the left. The functionality of Robert Webb's original script is maintained by entering "0" for the word# count. Example: ID Name PIN E-mail 172987129 Jon Doe 5787 jondoe@spamme.com 943973494 Don Juan Marco Jr 2001 don@nonexistent.net 439872390 Bob Peter Tomalin 7786 tomalin@nospam.edu To sort by ID, enter "1" for word number. You can also enter a word number count from right by entering a negative number. Since the Name column has a variable number of words for each line, in order to sort by PIN, enter "-2" to indicate the second word from the right. After that, you will be prompted to enter the sort order. For example, enter "r" to sort in reverse order. "Put in vimrc file - tested with GVim 6.3 " use visual block to select lines to sort and hit vmap :call Sort(Prompt("0","1"),Prompt("1","f"),"Strcmp") "sort lines function function Sort(wnum, order, cmp) range call SortR(a:firstline, a:lastline, a:wnum, a:order, a:cmp) normal gv endfunction "sort lines recursively function SortR(start, end, wnum, order, cmp) if a:start >= a:end return endif let partition = a:start - 1 let middle = partition let partstr2 = Words2(getline((a:start + a:end) / 2), a:wnum) let i = a:start while i <= a:end let str = getline(i) let partstr = Words2(str, a:wnum) if a:order == "r" execute "let result = ".a:cmp."(partstr2, partstr)" else execute "let result = ".a:cmp."(partstr, partstr2)" endif if result <= 0 "swap i before partition let partition = partition + 1 if result == 0 let middle = partition endif if i != partition let str2 = getline(partition) call setline(i, str2) call setline(partition, str) endif endif let i = i + 1 endwhile "make sure middle element at end of partition if middle != partition let str = getline(middle) let str2 = getline(partition) call setline(middle, str2) call setline(partition, str) endif call SortR(a:start, partition - 1, a:wnum, a:order, a:cmp) call SortR(partition + 1, a:end, a:wnum, a:order, a:cmp) endfunction "determine compare strings function Words2(line, wnum) if a:wnum > 1 return strpart(a:line, matchend(a:line, "\\s*\\(\\S*\\s*\\)\\{".(a:wnum - 1)."}")) elseif a:wnum == 1 return strpart(a:line, matchend(a:line, "\\s*")) elseif a:wnum < 0 return matchstr(a:line, "\\(\\S*\\s*\\)\\{".(-a:wnum)."}$") else return a:line endif endfunction "compare two strings function Strcmp(str1, str2) if a:str1 < a:str2 return -1 elseif a:str1 > a:str2 return 1 else return 0 endif endfunction "prompt user for settings function Prompt(str, ...) let default = a:0 ? a:1 : "" if a:str == "0" let str = "Sort by which word [(0)whole line (<0)count from right]? " elseif a:str == "1" let str = "Order [(f)orward (r)everse]? " endif execute "let ret = input(\"".str."\", \"".default."\")" return ret endfunction Other references: * VimTip588 * VimTip800
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