Powershell partial identical values two csv files


I have two quite large csv files that contain values like this:

File one:

file two:
000074 Amsterdam,On
00C0AF Berlin,Add

Is there a way that I can make powershell compare the first six digits of file one and two an than combine those in a new file, so it give me:

000074-e8ec29,33,000074 Amsterdam,On
00c0af-012651,28,00C0AF Berlin,Add

Thanks in advance !!
Who is Participating?

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

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.

Dan CraciunIT ConsultantCommented:
Not directly.
But you can use pretty much any programming language (including powershell) to:
- read the first file line by line
- for each line search in the second file for a line starting with the first 8 characters of the current line
- if found, add it to the current line and write it to another file

If no one shows with a script by tonight I'll write one.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Does "quite large" mean that we cannot cache at least one of them in memory? If we can't, a slow, filesystem based merge have to be done.
Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Surely you can just first divide them into 2 parts with Data "-" this can now in 2 sheets be used to do Vlookup

- Rancy
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Manpreet SIngh KhatraSolutions Architect, Project LeadCommented:
Excel with your exaamples above

- Rancy
LoyallAuthor Commented:

I,m sorry for my delayed response. Personal issues took control.
Dan, is your offer to write a script still valid ?

Kind regards,
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:

please answer my question in http:#a41238506, so we can suggest a best-effort script (slow and file based versus fast and memory based).
LoyallAuthor Commented:
Hi Qlemo,

The files can be cached in memory.

Kind regards,
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
$fileOne = 'C:\Temp\FileOne.csv'
$fileTwo = 'C:\Temp\FileTwo.csv'
$result  = 'C:\Temp\MergedFile.csv'

$hashFTwo = @{}
Import-Csv $fileTwo -Header ID, Text -Delimiter ' ' | % { $hashFTwo += @{$_.ID = $_.Text } }
Import-CSV $fileOne -Header id, text -Delimiter '-' |
% {
  $_.ID +'-'+ $_.Text + ',' + $_.ID +  ' ' + $hashFTwo[$_.ID]
} | Out-File -Encoding ASCII MergedFile.csv

Open in new window

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
LoyallAuthor Commented:
Thank you very much Qlemo !!
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

From novice to tech pro — start learning today.