INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Merging CSV columns

Merging CSV columns

(OP)
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 --> powershell

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close