Merging two CSV files

Hey there,
once again I’ve a question.
I’ve two CSV files with a URL and a Timestamp. But I want to merge them, but only with unique entrys and if the URL is existing in both files it should take the newer file (tmp-file)

Get-Content $timestamp_csv, $timestamp_csv_tmp |
  Select-Object -Unique |
    Set-Content  $timestamp_csv
remove-item $timestamp_csv_tmp -force -Confirm:$false

$Timestamp_csv is the old file and $timestamp_csv_tmp is the new one. But if lines with URLs are the same it should take $timestamp_csv_tmp

How can I achive this?

If you have proper CSV files you should treat them as such and use Import-Csv instead of Get-Content.

If they are the same why does it matter which one you use? :wink:

You can use

to compare two arrays. Please read the help completely inclusding the examples to learn how to use it.

If you need further assistance you should share some sample data of your input files as well (formatted as code as well please) and maybe a sample of thexpected result.

Thanks in advance.

Hi Olaf,

thanks again :wink: Didn’t know that compare-Object could compare so many things - thanks for the hint :slight_smile:

This is my working code that does what it should :smiley:

$csv1 = import-csv $timestamp_csv
$csv2 = import-csv $timestamp_csv_tmp

$WorkingArray = import-csv $timestamp_csv
$OutputFile=Compare-Object -ReferenceObject $csv1 -DifferenceObject $csv2 -PassThru -Property "URL", "Timestamp"
Foreach ($line in $OutputFile) {
    # => is missing in ref obj
    # <= is missing in diff obj
    if ($line.sideindicator -eq "=>"){
        $WorkingArray  = $WorkingArray  | where-object {$_.URL -notmatch $line.URL }
            URL = $line.URL
            timestamp = $line.Timestamp
        $WorkingArray += New-Object psobject -Property $objproperties 
$WorkingArray | export-csv $timestamp_csv
remove-item $timestamp_csv_tmp -force -Confirm:$false