Consolidating lines in csv file

Welcome Forums General PowerShell Q&A Consolidating lines in csv file

Viewing 5 reply threads
  • Author
    Posts
    • #236470
      Participant
      Topics: 1
      Replies: 4
      Points: 25
      Rank: Member

      Hey guys

      I have a large CSV file (approx 15,000 rows) that contains a foldername and a username on each line. My goal is to end up with 1 line per foldername along with all of the usernames.

      example file

      desired file

       

      Here’s my code so far and although it works – it takes forever as i’m constantly rereading $data.

      I’m hoping somebody can help make my code more efficient, allowing the entire task to take minutes instead of hours.

       

      Thank you in advance! 🙂

    • #236476
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Robbie, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      The way you wanted to do would not provide a valid CSV file. Try this:

      Of course you should provide your source data with an Import-Csv. 😉 … the result would look like this:

    • #236539
      Participant
      Topics: 1
      Replies: 4
      Points: 25
      Rank: Member

      Hey Olaf,

      Thank you very much my friend. I have now fixed my original post 🙂

      Your code was of great help! I had completely forgotten about the Group-Object cmdlet.

      Here’s my new code, it now takes approximately 90 seconds to run!!!!

      Thanks again!
      Robbie

       

       

    • #236746
      Participant
      Topics: 0
      Replies: 38
      Points: 378
      Helping Hand
      Rank: Contributor

      Hello Robbie,

      If you would change your $fullinfo to ArrayList ( System.Collections.Arraylist) and instead of using += would use ArrayList.Add() it will be even faster.
      Like this:

      Hope that helps.

      • This reply was modified 3 months ago by AndySvints.
    • #236764
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      If you would change your $fullinfo to ArrayList (System.Collections.Arraylist) and instead of using += would use ArrayList.Add() it will be even faster.

      There’s an even easier way than that:

    • #236809
      Participant
      Topics: 1
      Replies: 4
      Points: 25
      Rank: Member

      Thank you gents, both are great options! 🙂

Viewing 5 reply threads
  • The topic ‘Consolidating lines in csv file’ is closed to new replies.