Finding matching in a Cell

Welcome Forums General PowerShell Q&A Finding matching in a Cell

Viewing 3 reply threads
  • Author
    Posts
    • #257807
      Participant
      Topics: 6
      Replies: 9
      Points: 63
      Rank: Member

      I am comparing two csvfile and want to find a match in a cell.

      Example

      Machine Name Location Status
      JOEDOE1

       

      Computerrname Location Status
      JOEDOE2, JOEDOE3, Jimbob, Jerrry NY Retired
      JImbob2, jeery2, JOEDOE1 NY Active

      My Script

      I tried different variable like -match, -contain, but it cant read inside that cell to find a match. Any suggestion?

      • This topic was modified 1 month ago by tn647883.
      • This topic was modified 1 month ago by grokkit. Reason: code formatting - please read the guide
      • This topic was modified 1 month ago by tn647883.
    • #257834
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      If this your entire script, there are several issues.

      1. You do not set variable r or computername, but try to use them.
      2. You are missing closing curly braces.
      3. The reference CSV is not a good example of CSV. See below
      4. You are over complicating it.

      Reference CSV should look like this

      Computerrname Status
      JOEDOE2 Retired
      JOEDOE3 Retired
      Jimbob Retired
      Jerrry Retired
      JImbob2 Active
      jeery2 Active
      JOEDOE1 Active

      However, working with the data you have, if the goal is to output a new CSV with the computername (from computername CSV) and the status (from matched line in reference CSV) then this simplified version will do the work.

      Which outputs (and also captures to $newCSVdata variable)

      You can add $newCSVdata | Out-Gridview or $newCSVdata | Export-CSV -Path \some\path\file.csv -NoTypeInformation to the end.

    • #257861
      Participant
      Topics: 6
      Replies: 9
      Points: 63
      Rank: Member

      Sorry for the typo and syntax error above. I have corrected it and add additional info. My issue is i am working with over 5000 rows with 10 column and the header names do not match but are similar.

    • #257876
      Senior Moderator
      Topics: 10
      Replies: 184
      Points: 980
      Helping Hand
      Rank: Major Contributor

      My issue is i am working with over 5000 rows with 10 column and the header names do not match but are similar.

      You probably are overthinking this. The easiest fix would be to pick one of the CSV files and edit its header names to match those of the other CSV so that you can run the comparison (you need to normalize the data before you can perform useful tasks with it).

      If the headers are always consistent for that file, you could script the header renaming task so that you don’t have to do it manually.

      • This reply was modified 1 month ago by grokkit.
Viewing 3 reply threads
  • You must be logged in to reply to this topic.