Find and replace strings with REGEX and incremental replacement string

Welcome Forums General PowerShell Q&A Find and replace strings with REGEX and incremental replacement string

Viewing 6 reply threads
  • Author
    Posts
    • #277782
      Participant
      Topics: 2
      Replies: 9
      Points: 40
      Rank: Member

      Hello,  I want to find and replace multiple strings (multiple patterns ), and some of the strings are on the same line. The replacement value should be incremental so this will allow to keep track in separate file on original / replaced values.  I need some help to make my code to achieve that.

      An example input is:

      Desired output:

      And the code on which I am working on:

       

       

    • #277932
      Participant
      Topics: 4
      Replies: 422
      Points: 734
      Helping Hand
      Rank: Major Contributor

       

      • #278205
        Participant
        Topics: 2
        Replies: 9
        Points: 40
        Rank: Member

        Thanks Matt! How will you modify the script if there are 2 or more patterns? Here reportId {\w19} and KeyID {\w32}? Then how can I export the hash-table with “original” and “replaced” values to keep them in a file (e.g. “qwerty-qwer12-qwer56 : Request-1”)?

    • #278091
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      That appears to be xml. Rather than attempting to manipulate a string, why not use xpath or xml dot notation?

      Mastering everyday XML tasks in PowerShell

      • #278202
        Participant
        Topics: 2
        Replies: 9
        Points: 40
        Rank: Member

        Hi Rob, no it is not XML. Its big log file with a lot of data, some of it with some structure.

    • #278199
      Participant
      Topics: 2
      Replies: 9
      Points: 40
      Rank: Member

      Thanks Matt! How will you modify the script if there are 2 or more patterns? Here reportId {\w19} and KeyID {\w32}? Then how can I export the hash-table with “original” and “replaced” values to keep them in a file (e.g. “qwerty-qwer12-qwer56 : Request-1”)?

    • #278211
      Participant
      Topics: 4
      Replies: 422
      Points: 734
      Helping Hand
      Rank: Major Contributor

      Select-String should accept an array of strings for the Pattern, although that didn’t seem to work.  However, you can put two patterns in the regex which is what I’ve done.

      I also added a Select-Object -Unique so that the count matches the replacement value if there’s more than one of the same value.

      To keep the changes, I opted to use a custom object and export it to a CSV file as it’s easy to read.

       

      • #278220
        Participant
        Topics: 2
        Replies: 9
        Points: 40
        Rank: Member

        Thanks Matt for the prompt reply. However I have 5 more patterns to replace. There should be some sort of loop for the Select-String for each pattern and I need to keep some identification per type. My target is to achieve the following result after running the code:

        Do you have an idea how to achieve that? Many thanks in advance!

    • #278325
      Participant
      Topics: 4
      Replies: 422
      Points: 734
      Helping Hand
      Rank: Major Contributor

      In that case, I would match the tag as well then use multiple counters:

       

      • This reply was modified 1 month, 1 week ago by Matt Bloomfield. Reason: Reformatted a long line for readability
      • #278724
        Participant
        Topics: 2
        Replies: 9
        Points: 40
        Rank: Member

        Perfect solution Matt! Thanks a lot. Can I ask one last question on this example? Can the “export” section be dependent on a variable so if needed to export (e.g. $export=Y) then the export section is considered and on contrary if $export=N it is skipped? Maybe the export will have an impact on the server performance that’s why I am considering this option. Many thanks for your support! 

      • #279114
        Participant
        Topics: 2
        Replies: 9
        Points: 40
        Rank: Member

        Dear Matt, I notice that with the new pattern (<RequestId>\w{6}-\w{6}-\w{6}) the “Order: Q2we45-Uj87f6-gh65De<” is not detected. I tried to remove <requestId> from the Select-String pattern but then I got errors. What should I modify? It is OK that OrderId, RequestId and messageId match the same pattern – each of them will return RequestId-x. I am also struggling to replace the “user” (user 1 or user-1 or user_1 are the same) by “customer”. Can you help with these 2 points please? Here is updated entry array:

      • #279129
        Participant
        Topics: 4
        Replies: 422
        Points: 734
        Helping Hand
        Rank: Major Contributor

        I recommend using regex101.com to test and build your regular expressions:

        https://regex101.com/

        It’s not clear if you want the RequestId and Order in the same string or not?

        Would match the whole string up to the end of the order number.

        Would match just the order portion.

        For the users, you just need to find a regular expression that matches your examples.

        Would match User, followed by any character, followed by 1 or more digits.

         

         

      • #279189
        Participant
        Topics: 2
        Replies: 9
        Points: 40
        Rank: Member

        Thank you Matt for the prompt feedback. I tried with the suggested pattern I probably did something wrong in the foreach part as it didn’t capture everything:

        Regarding the strings RequestID, OrderId and messageID, YES, I want to capture them in the same string/pattern. I wonder if it will be possible to get rid of ReportId and OrderId and to have the pattern in this form (but I am not sure how to modify the foreach part):

        And at last , for the “User” a good pattern is the one below but I cannot make it working

        or that one:

        Here is my new test input:

        And my updated desired output:

        Thanks in advance for your help.

        • This reply was modified 1 month ago by Vassil96.
        • This reply was modified 1 month ago by Vassil96.
      • #279252
        Participant
        Topics: 4
        Replies: 422
        Points: 734
        Helping Hand
        Rank: Major Contributor

        You can just add the order part to the existing part that replaces the request id.

        The users are a bit trickier because you have an inconsistent format.  I would make that consistent first as it makes the find and replace simpler.

         

         

        • This reply was modified 1 month ago by Matt Bloomfield. Reason: Tried changing theme for better highlighting
      • #280512
        Participant
        Topics: 2
        Replies: 9
        Points: 40
        Rank: Member

        Big Thanks Matt.

        These days, quite busy, I am working to adapt your solution to my real data :).

      • #280581
        Participant
        Topics: 2
        Replies: 9
        Points: 40
        Rank: Member

        Is it possible to change the pattern related to “requests and orders” only to

        This will cover all requestId’s and Order’s. How then the “foreach” part has to be modified?
        I tried the following code but with no success:

        • This reply was modified 1 month ago by Vassil96.
        • This reply was modified 1 month ago by Vassil96.
      • #280710
        Participant
        Topics: 4
        Replies: 422
        Points: 734
        Helping Hand
        Rank: Major Contributor

        That modification worked OK for me and matched your desired output.

    • #278742
      Participant
      Topics: 4
      Replies: 422
      Points: 734
      Helping Hand
      Rank: Major Contributor

      For sure.  Just add the variable at the top of the script:

      Then modify the export section:

       

       

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