Script merge CSV files

I have a few .CSV files that I want to merge together (sample files attached).
The 3 .CSV files have different fields (in a different order).
I only want to merge 3 common fields (email_address, state, zip) from the files into a 4th output .CSV file.
Any ideas on how to do this with either VBScript or Windows Batch?  
Thanks for any help.
File1.csv
File2.csv
File3.csv
LVL 3
fjkaykr11Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
Make a .bat file of this:
set newcsv=merged.csv
echo email_address,State,zip>>%newcsv%
for /f "skip=1 tokens=6-11 delims=," %%a in (File1.csv) do echo %%a,%%e,%%f>>%newcsv%
for /f "skip=1 tokens=7-12 delims=," %%a in (File2.csv) do echo %%a,%%e,%%f>>%newcsv%
for /f "skip=1 tokens=4-9 delims=," %%a in (File3.csv) do echo %%a,%%e,%%f>>%newcsv%

Open in new window

0
fjkaykr11Author Commented:
@NewVillageIT thanks so much, it worked great!
I might have to alter the batch file a bit based on the other CSV files I need to work with.   So I am trying to interpret how the batch file determines the 3 fields to output
Could you please explain that part.   Thanks again
0
NVITEnd-user supportCommented:
The first line makes the header:
echo email_address,State,zip>>%newcsv%

The next 3 are similar. The difference is where it starts to make the variables. e.g. for the first line...

skip=1 means to skip the first line, i.e. skip the header. We already have that covered.

tokens=6-11 means start our variable declarations at the 6th field, i.e. email_address. Then, make room for 6 variables 6 to 11. This means 6 becomes %%a, 7=%%b, 8=%%c, etc. As you can see, the numbers of each line per file1, file2, and file3 differ because the field location in each file differs.

delims=, means use the commas are field delimiters in file1.csv

Does that help?

for /f "skip=1 tokens=6-11 delims=," %%a in (File1.csv) do echo %%a,%%e,%%f>>%newcsv%
for /f "skip=1 tokens=7-12 delims=," %%a in (File2.csv) do echo %%a,%%e,%%f>>%newcsv%
for /f "skip=1 tokens=4-9 delims=," %%a in (File3.csv) do echo %%a,%%e,%%f>>%newcsv%
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fjkaykr11Author Commented:
thank you very much for the great explanation.  I really appreciate it.
0
NVITEnd-user supportCommented:
You're very welcome. I enjoyed it. Have a great weekend...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.