我想知道是否有一个Windows命令可以添加两个不同的值,这些值在文件中具有相同的ID。
例如:
文件:
id;value
01;25
02;12
01;2
03;21
03;-5
结果:
id;value
01;27 /* 25 + 2 */
02;12
03;16 /* 21 - 5 */
对于需要答案的人,我找到了一种方法:
setlocal EnableDelayedExpansion
echo off
rem write header in output file
echo id;value> output.csv
rem remove the header of the file and loop
for /f "tokens=1,2 delims=;" %%A in ('more +1 my_file.csv') do (
set "result="
rem search if current id is in the temp list (tmp file) of processed ids
for /f "delims=" %%i in ('findstr /c:"%%A;" output.csv') do set result=!result!%%i
rem if the id is not in tmp file (the id is not yet processed)
if "!result!"=="" (
set sum=0
rem get all values related to the current id and calculate the sum
for /f "tokens=2 delims=;" %%V in ('findstr /c:"%%A;" my_file.csv') do (
set /a sum+=%%V
)
echo %%A;!sum!>> output.csv
)
)