3 ms·
The filter command in vim allows you to use any external program to manipulate the contents of the buffer. So, if you have a CSV or TSV and want to do somethin
by u801e 8y ago
The filter command in vim allows you to use any external program to manipulate the contents of the buffer. So, if you have a CSV or TSV and want to do something like compute the sum of numbers in a column, you could write a awk or perl one-liner with the appropriate field-separator parameter and do it.
So, for a CSV, you could do something like this in perl
:% !perl -F/,/ -ane '$total += $F[3] if $F[4] =~ /Merchandise/; END {print $total;}'
This command will filter every line in the vim buffer through the perl one-liner. The perl one-liner splits the line by comma, and adds the amount in the fourth column to the total amount if the fifth column contains the word "Merchandise". The lines in the vim buffer are replaced by the computed total. I actually use this to determine how much I spend for each category in my Discover Card statement after downloading it as a CSV.
- chongli 8y agoThis won't work if there are commas within fields, as permitted by CSV quoting rules.