Retrieving two lines from text files in folder and appending to a file

Welcome Forums General PowerShell Q&A Retrieving two lines from text files in folder and appending to a file

Viewing 8 reply threads
  • Author
    Posts
    • #249464
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      I need to pull out two lines of text from multiple text files and put them into one CSV file.

      =============================

      Sample data:

      File1.txt

      User=asfd
      UserID=12
      Computer=12343
      Name=Smith, Will

      File2.txt

      User=qwerty
      UserID = 14
      Computer=4567
      Name=Smith, John

      File3.txt

      User=asfd
      UserID=12
      Computer=123434
      Name=Smith, William

      =============================

      I want to pull out the two lines that are UserID & Name and put those into a CSV that is structured like this:

      I am currently using:

      but I can’t put this into the same file as

      Any suggestions?

      • This topic was modified 1 month, 1 week ago by aarongcastillo17. Reason: Formatting on PoSH cmdlets
    • #249482
      Participant
      Topics: 0
      Replies: 1
      Points: 6
      Rank: Member

      It’s certainly not a one-liner, but it uses Regex and parses the files into their own PSObjects (which can then exported to CSV).

      • This reply was modified 1 month, 1 week ago by Yevrag35.
    • #249506
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      Not counting string splitting/manipulation, I see 3 possible solutions: using Select-String with regex like you are, ConvertFrom-String, and ConvertFrom-StringData.

      Select-String

      [/crayon]

      Output

      [/crayon]

      ConvertFrom-String

      Now this one assumes you have more than one entry per file, otherwise why are you automating it? 🙂 Need to give enough training data without going overboard. For this I have combined the 3 users into one file. Closer to real sample data may be better to train with.

      [/crayon]

      The contents of the csv

      [/crayon]

      I haven’t quite figured ConvertFrom-StringData out all the way. I say it seems like a good fit because the data is already in the “name=value” format. Maybe someone else can add an example using that.

    • #249554
      Participant
      Topics: 0
      Replies: 7
      Points: 45
      Rank: Member

      ConvertFrom-StringData will convert a string into a hash table, but the key has to be unique. In the “alltext.txt” example that Doug gives, ConvertFrom-StringData will fail as the keys (ie User in User=qwery) has already been defined (User=asfd).

      If they are all seperate files, something like this should work;

    • #249668
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member

      Thank you all so much, you got me going in the right direction!

    • #249716
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      Good deal. Just to be thorough for myself and future readers, here is another way using plain ol’ string manipulation.

      [/crayon]
    • #250091
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member
    • #250103
      Participant
      Topics: 7
      Replies: 557
      Points: 2,122
      Helping Hand
      Rank: Community Hero

      It just linked to this thread, so not sure which one you used. I also wanted to share my ConvertFrom-StringData solution.

      [/crayon]

      Output

      [/crayon]

      To limit to just the 2 desired properties

      [/crayon]

      As desired

      [/crayon]

      That worked fine because there is only one set per file. However it’s likely to have many per file. If the files hav each set separated by a blank line, you can use this approach.

      [/crayon]

      This will split each group at the blank line and then process each line of each group the same way as before.

    • #250166
      Participant
      Topics: 1
      Replies: 3
      Points: 19
      Rank: Member
Viewing 8 reply threads
  • You must be logged in to reply to this topic.