Rao
Rao

Reputation: 1

Powershell Copy the CSV output and paste in second column of another CSV

It's regarding powershell.

I have csv file with name a and it contains values 1 2 3 4 5 in first column and I have another csv file with name b and it contains values 6 7 8 9 10.

b.csv file output as follows.

6  
7                                                                                                            
8                                                                                                                                                                                                                
9                                                                         
10

a.csv file output as follows.

1  
2                                                                                                            
3                                                                                                                                                                                                                
4                                                                         
5

Desired result should be as follows

1  6  
2  7                                                                                                          
3  8                                                                                                                                                                                                              
4  9                                                                       
5  10

We are copying the output of b.csv file to a.csv file in column2.

Upvotes: 0

Views: 248

Answers (1)

Lee_Dailey
Lee_Dailey

Reputation: 7489

here's one way to do the job. [grin] it presumes the two collections have the same number of items. if not, there will be errors.

# fake reading in a CSV file
#    in real life, use Import-CSV
$A_In = @'
1
2
3
4
5
'@ | ConvertFrom-Csv -Header 'A_Col'

# fake reading in another CSV file
$B_In = @'
6
7
8
9
10
'@ | ConvertFrom-Csv -Header 'B_Col'

# the ".GetUpperBound(0)" stuff gets the upper bound of the zero axis of the collection
#    that is the same as ".Count - 1"
$Results = foreach ($Index in 0..$A_In.GetUpperBound(0))
    {
    [PSCustomObject]@{
        A_Col = $A_In[$Index].A_Col
        B_Col = $B_In[$Index].B_Col
        }
    }

$Results

output ...

A_Col B_Col
----- -----
1     6    
2     7    
3     8    
4     9    
5     10   

at this point, the $Results collection can be gracefully sent to a CSV via Export-CSV.

Upvotes: 1

Related Questions