Solved How I replace cell values of a column of a CSV from another?

November 5, 2018 at 05:42:12
Specs: Windows 10
Need the script in powershell or vb script

Ex.

XX.csv is like

A B C
aa bb cc

YY.csv is like

A B
aa gg

My output csv file would be like:-
A B C
aa gg cc

message edited by SantanuC


See More: How I replace cell values of a column of a CSV from another?

Reply ↓  Report •

✔ Best Answer
November 5, 2018 at 06:53:03
below is working code, current output=
A B C
A.pv small 133
A.pv big 298

obviously replace these values;
set file1=XX.csv
set file2=YY.csv
set outputfile=final.csv

::start of test.bat
@echo off&setlocal enabledelayedexpansion
set file1=XX.csv
set file2=YY.csv
set outputfile=final.csv
:MAIN
set line=0
for /f "tokens=1,3" %%a in (%file1%) do (
call :READ2ndtoken %%a %%b !line!
set/a line+=1
)
exit/b
:READ2ndtoken
if !line!==0 (
for /f "tokens=2" %%A in (%file2%) do echo %1 %%A %2 >> %outputfile%&exit/b
) else (
for /f "skip=%3 tokens=2" %%A in (%file2%) do echo %1 %%A %2 >> %outputfile%&exit/b
)
exit/b

::end of test.bat

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1400Mhz core@1.18v/1950MHz



#1
November 5, 2018 at 05:47:57
or batch :)

can you give examples (multiple lines of text) from both XX.csv and YY.csv

also both file need to be merged into output.csv or do you want 2 output.csv files?

edit: im just gonna start scripting, so far i understand output.csv should contain the contens of XX.csv and the values should be overwritten by YY.csv

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1400Mhz core@1.18v/1950MHz

message edited by hidde663


Reply ↓  Report •

#2
November 5, 2018 at 06:00:31
yes, or batch will also work. :) and in one file as a final csv. Pls. see as below..

Read and Write should be replaced by Big and Small

ABC.csv
------------
A B C
A.pv Write 133
A.pv Read 298

EFG.csv
------------
A B
A.pv small
B.pv big

Final.csv
------------

A B C
A.pv small 133
B.pv big 298

message edited by SantanuC


Reply ↓  Report •

#3
November 5, 2018 at 06:14:12
max words on 1 line = 3? or are there lines with more than 3, if so how many is the max?

edit:

run test.bat from the folder where the .csv files are located & replace XX.csv & YY.csv with the appropriate filenames:

::test.bat
@echo off
for /f "tokens=1,3" %%a in (XX.csv) do (
for /f "tokens=2" %%A in (YY.csv) do (
echo %%a %%A %%b >> final.csv
)
)

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1400Mhz core@1.18v/1950MHz

message edited by hidde663


Reply ↓  Report •

Related Solutions

#4
November 5, 2018 at 06:26:06
above code has some issues, just tested it.

im moving too fast, ill report back in 15min or more with some decent code.

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1400Mhz core@1.18v/1950MHz


Reply ↓  Report •

#5
November 5, 2018 at 06:53:03
✔ Best Answer
below is working code, current output=
A B C
A.pv small 133
A.pv big 298

obviously replace these values;
set file1=XX.csv
set file2=YY.csv
set outputfile=final.csv

::start of test.bat
@echo off&setlocal enabledelayedexpansion
set file1=XX.csv
set file2=YY.csv
set outputfile=final.csv
:MAIN
set line=0
for /f "tokens=1,3" %%a in (%file1%) do (
call :READ2ndtoken %%a %%b !line!
set/a line+=1
)
exit/b
:READ2ndtoken
if !line!==0 (
for /f "tokens=2" %%A in (%file2%) do echo %1 %%A %2 >> %outputfile%&exit/b
) else (
for /f "skip=%3 tokens=2" %%A in (%file2%) do echo %1 %%A %2 >> %outputfile%&exit/b
)
exit/b

::end of test.bat

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1400Mhz core@1.18v/1950MHz


Reply ↓  Report •

#6
November 5, 2018 at 09:59:32
Should I point out I've done something like this 2 years ago? https://www.computing.net/answers/p...

How To Ask Questions The Smart Way


Reply ↓  Report •

#7
November 5, 2018 at 13:20:29
probably :)  

i5-6600K[delid]@4.8GHz/4.4GHz@1.424v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-28 1T 2800MHz@1.37v
MSI Armor RX 570 4GB@1400Mhz core@1.18v/1950MHz


Reply ↓  Report •

Ask Question