merge two .csv files on common column using dos

March 1, 2013 at 21:05:26
Specs: Windows 7
HI,

I have two .csv files.

for e.g.
file1.csv

ID,Value1
1,100
3, 150
4, 200

file2.csv

ID,Value2
1,180
2, 40
4, 60

Required output(order does not matter)
ID,Value1,Value2
1,100,180
3, 150,0
4, 200,60
2,0,40



See More: merge two .csv files on common column using dos

Report •

#1
March 2, 2013 at 11:47:40
This works in my tests, but it may be too slow for large files and the code is roinky as hell:
::===== begin batchscript merg.bat
@echo off & setlocal
(for /f "skip=1 tokens=1,2 delims=," %%a in (file1.csv) do (
for /f "tokens=1,2 delims=," %%c in ('findstr /b /c:"%%a" file2.csv^|^|echo x^,0') do (
echo %%a,%%b,%%d
)
)
)>merged.csv

(for /f "skip=1 tokens=1,2 delims=," %%a in (file2.csv) do (
findstr /b /c:"%%a" file1.csv>nul||echo %%a,0,%%b
)
)>>merged.csv
::====== end batch
I'm sure there's a better way to do this (esp. with powershell, which I haven't aquired yet). I just can't think of it.


Report •
Related Solutions


Ask Question