Finding a variable text in another textfile and get the containing line

Welcome Forums General PowerShell Q&A Finding a variable text in another textfile and get the containing line

Viewing 3 reply threads
  • Author
    Posts
    • #257663
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      Hello there,

      quite new to Powershell, i’m struggling with the following problem and im not finding any good answer in the interwebs.

      I have a textfile “ID.txt” containing lines with IDs in form of “SBS1234568” in variable length.

      I have another textfile “IPs.txt” containing lines with IP adresses and the mentioned ID above separated by “;” aka “10.55.0.0/24 ; SBS1234568”

      I now want to find IDs listet in “id.txt” in “IPs.txt” and get the entire line from “IPs.txt” to output it to another file.

      I tried with a foreach and IF but I was not able to get it anywhere near…

      EDIT:
      This is what I tried so far:

       

      Any help would be very much appreciated!

      regards blitzdesigner

      • This topic was modified 1 month ago by blitzdesigner71. Reason: added code
    • #257669
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

      https://stackoverflow.com/questions/63981336/finding-a-variable-text-in-another-textfile-and-get-the-containing-line

      Thanks

    • #257675
      Participant
      Topics: 1
      Replies: 1
      Points: 14
      Rank: Member

      you are right, sorry 🙂

    • #257702
      Participant
      Topics: 8
      Replies: 617
      Points: 2,393
      Helping Hand
      Rank: Community Hero

      For completeness I am providing the same answer I shared on StackOverflow.com here.

      You can use Select-String on the IPs.txt file but one important thing you’re missing is your text in ID.txt is in this format

      Therefore when you’re trying to match a line in IPs.txt, it is effectively this

      “10.55.0.0/24 ; SBS1234568 -match SBS1234568  # this is $false

      You’d have to remove at least the first quote, but instead of doing all that I would treat it as a CSV.

      Output

      &#8220;10.55.0.0/24 ; SBS1234568&#8221;<br />&#8220;10.44.0.0/24 ; SBS345689123&#8221;<br />&#8220;10.33.0.0/24 ; SBS87654&#8221;<br />&#8220;10.22.0.0/24 ; SBS87612354&#8221;

Viewing 3 reply threads
  • You must be logged in to reply to this topic.