:r!awk '{sum+=$6} END {print "Total: "sum}' %
Wyjaśnienie:
:r ........... read (put result in this file)
! ............ external command
awk .......... external tool
{sum+=$6} .... sixth field (awk considers spaces as field separator)
END .......... at the end
{print "Total: "sum} --> string "Total: " plus your result
% ............ current file
Próbowałem funkcji, która działa tutaj:
" This function requires you select the numbers
fun! SumVis()
try
let l:a_save = @a
norm! gv"ay
let @a = substitute(@a,'[^0-9. ]','+','g')
exec "norm! '>o"
exec "norm! iTotal \<c-r>=\<c-r>a\<cr>"
finally
let @a = l:a_save
endtry
endfun
vnoremap <leader>s :<C-u>call SumVis()<cr>
Korzystając z powyższej mapy, wszystko co musisz zrobić po załadowaniu funkcji, to wybrać liczby, które chcesz zsumować i użyć, <leader>s
aby zsumować wybrany obszar.
Objaśnienie funkcji:
Używa try/finally/endtry
ekstrakcji do wychwytywania błędów.
let l:a_save = @a .......... if whe have register 'a' we save it temporarelly
norm! gv"a ................................... gv --> reselects and captures selection to 'register a'
let @a = substitute(@a,'[^0-9. ]','+','g') .... removes all but numbers, dots and spaces from 'register a' and puts '+' among the numbers
exec "norm! '>o" ............................. opens new line bellow selection. see :h '>
exec "norm! iTotal: \<c-r>=\<c-r>a\<cr>" ...... insert "Total: " plus 'expression register result
let @a = l:a_save ............................. restores original 'a' register content
Jeśli chcesz wypróbować tę funkcję, wykonaj następujące czynności: Skopiuj tę funkcję w przeglądarce i uruchom to polecenie na vimie, :@+
co pozwoli ci :call SumVis()
normalnie korzystać .
:@+ ......... loads `+` register making the function avaiable
Musi dokonać wizualnego wyboru bloku za pomocą ctrl+ v, odznaczyć i wreszcie wywołać funkcję. Lub możesz użyć sugerowanej mapy, która sama usuwa zaznaczenie przed obliczeniem.