Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging CSV columns

Status
Not open for further replies.

Jtable

Programmer
Sep 12, 2013
1
Hi all,

I have multiple CSVs which all start with a "Time" column and then contain different other columns. I cannot know in advance the column names nor do I care about them.
What I am try to do is to create a loop that would do something like this :

Code:
ForEach ($Server in $ServerList)
{
  $ServerPath=".\logs\$Server-file1.csv"
  $TmpCSV=Import-Csv -Path $ServerPath
  if ($CSVArray -eq $null) {
    #--> Get first column from $TmpCSV and store it as first column of $CSVArray
  }
  #--> Get columns 1 through 5 from $TmpCSV, add the $Server variable to their header, and store them to the right of the last column of $CSVArray
}
$CSVArray |Export-Csv outfile.csv -NoTypeInformation

With a $ServerList that looks like this :
Server1
Server2
Server3
...

And CSVs that look like this :
.\logs\Server1-file1.csv ------------
Time,toto,titi,tutu,tata,tete
11:00,3,5,4,2,1
11:10,2,5,1,4,3
...

.\logs\Server2-file1.csv ------------
Time,roro,riri,ruru,rara,rere
11:00,1,2,1,5,8
11:10,3,2,1,4,4
...

.\logs\Server3-file1.csv ------------
Time,popo,pipi,pupu,papa,pepe
11:00,3,2,1,4,4
11:10,2,9,1,2,3
...

I would expect this result :
outfile.csv ------------
Time,Server1-toto,Server1-titi,Server1-tutu,Server1-tata,Server1-tete,Server2-roro,Server2-riri,Server2-ruru,Server2-rara,Server2-rere...
11:00,3,5,4,2,1,1,2,1,5,8,3,2,1,4,4
11:10,2,5,1,4,3,3,2,1,4,4,2,9,1,2,3
...

Any idea as to how to handle this best ?

Thanks for your support

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top