2017-12-08 10 views
0

ファイル内に同じIDを持つ2つの異なる値を追加できる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 */ 

答えて

0

、私はそれを行う方法を見つけました:

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 
    ) 
) 
関連する問題