When Excel was my endpoint, I could lazy evaluate any sums by concatenating numbers as long expressions in the batch script:
for /f "delims=," %%i in ('findstr ...
echo !field1!,!field2!,!total!>> csv-file.txt
In VBA, opening the file caused Excel to evaluate the long strings of =0+4+8+12 in the cell as formulas. I didn’t have to worry about adding numbers in the batch script itself.
It’s a different story with perl and R. First, I had to strip the equal signs:
for /f "tokens=*" %%i in (file.txt) do (
for /f "delims== tokens=1-4" %%j in ("!line!") do (
echo %%j%%k%%l%%m>> for-R.txt
So cmd would strip the equals signs for me while dividing the line into chunks; I could keep my “x+y+z” expressions, and I would be home free. However, instead of evaluating the field as a single number, R read them as a “factor” type:
> x <- read.table(for-R.txt, sep=",", header=TRUE)
Levels 0+4+6 ...
Okay, opportunity knocked: I would use perl:
# ... code ...
open(my $filehdl, "<", "for-R.txt");
my @fields = split(',', $_);
my field1 = $fields
my calc = eval($field1); # 0+4+6
print $line . "\n";
Back in the batch script,
sum-cumul.pl !file! > for-R-2.txt
autoitmailer.exe arg1 arg2 arg3