On 11/18/2010 05:39 PM, sc wrote:
     #  sum last word
     { total += $NF }
     END { print total }

that sums the last word of each line (if it's numeric), how
can i feed it a range of lines and read the sum in after the
end?  reading the help it appeared i could enter

     :5,9r!awk -f ~/sumlw.awk

to read in the sum, but this hangs and i have to ctrl-c it to
regain control

The hang is awk waiting for input on stdin, but not receiving any from you (the user, not vim). So an EOF will also close awk but it will have processed whatever data you *typed* (likely nothing).

I don't know if your trailing data is integer or floating-point, but you have a couple options. My first thought is to use Vim's filter command ":!" and tweak your script so that the script regurgitates your input and then appends the total:

  =============sumlw.awk========
  { total += $NF ; print $0 ; }
  END { print total ; }
  ==============================

and then in vim issue

  :5,9!awk -f sumlw.awk

If your data contains integers (instead of decimal numbers), you can do it purely in vim:

:let total=0 | 5,9g/[-+]\=\d\+$/let total += matchstr(getline('.'), '[-+]\=\d\+$')
:put=total

If you have a more current version of vim that supports floating-point numbers, you can tweak the regexp from

  [-+]\=\d\+

to something like

  [-+]\=\d\+\%(\.\d\+\)

to capture stuff after a decimal point (additional work would be needed for scientific notation), and then you can use whatever constructs a float (I'm only running 7.1 here, and I think floats were added after that) wrapped around the matchstr() call.

-tim






--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to