Merge csv's - no join

ⅰ亾dé卋堺 提交于 2020-01-06 23:47:35

问题


I need to combine a slew of Excel spreadsheets. I used PowerSHell to convert them to CSVs and now need to merge them, but not as you typically would. The merge doesn't use a join. If I have 3 files with 100 rows each, my new file should have 300 rows. So, this is more if a UNION than a JOIN to use database terms.

Some of the columns do have the same name. Some don't. If they have the same name, a new column shouldn't be created. Is there a way to do this without manually having to list out all the columns as properties?

Example (with only 2 files)

File1:

Name Address 
Bob  123 Main

File2:

Name City
Bob  LA
Tom  Boston

Results

Name  Address City
Bob   123 Main
Bob           LA
Tom           Boston

回答1:


At the end of the day this might not be sorted right. The trick here is to read the header of each file and collect it as a string array and remove and of the duplicates.

This code assumes all the files are in the same location. If not you will need to account for that.

$files = Get-ChildItem -Path 'C:\temp\csv\' -Filter '*.csv' | Select-Object -ExpandProperty FullName

# Gather the headers for all the files. 
$headers = $files | ForEach-Object{
    (Get-Content $_ -Head 1).Split(",") | ForEach-Object{$_.Trim()}
} | Sort-Object -Unique

# Loop again now and read in the csv files as objects
$files | ForEach-Object{
    Import-Csv $_
} | Select-Object $headers 

The output would look like this:

Address  City   Name
-------  ----   ----
123 Main        Bob 
         LA     Bob 
         Boston Tom 


来源:https://stackoverflow.com/questions/33000221/merge-csvs-no-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!