Skip to content
loading...

Sum Last Field Using Vim

voyeg3r profile image Sérgio Luiz Araújo Silva twitter logo github logo ・1 min read

Let's say you have

val:1 5.50
this line has 6.70
this one, even more, fields 10.15

Using awk you refer to the last column with $NF. So after saving the file we can save the result ina variable

:let @a = system("awk '{t+=$NF} END {print t}' file.txt")

Now we insert the value with:

:put a

Or on insert mode

<Ctrl-r>a

A more simple solution

:r!awk '{t+=$NF} END {print "Total: " t}' %

:r ................... read to next line
awk .................. awk command
t  ................... variable to store total
$NF .................. last field
% .................... current file
twitter logo DISCUSS
Discussion
markdown guide
Classic DEV Post from Jul 30 '19

PublishTo.Dev: Scheduling article publishing on dev.to

Sérgio Luiz Araújo Silva profile image
I am a Free Software enthusiast and a (neo)?vim addicted, I also like shell script, sed, awk, and as you can see I love Regular Expressions.