Find and replace strings with REGEX and replacement string is incremental

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

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

      Hello, I have a big file with several patterns to be replaced. Some of the strings are on the same line and I also need to increment the replacement value. At last I would like to keep in a file the “matching/original” value and “replaced” value.  Any ideas how to achieve that?

      Example input:

      Desired output:

      And the code which I cannot make running is:

      @’
      <requestId>qwerty-qwer12-qwer56</requestId>Ace of Base Order: Q2we45-Uj87f6-gh65De<something else…
      <requestId>zxcvbn-zxcv12-zxcv56</requestId>reportId>poGd56Hnm9q3Dfer6Jh</msg:reportId>
      <requestId>1234qw-12qw12-123456</requestId>kljsldjslddsdfdsdsdfff <messageId>1234qw-12qw12-123456</msg
      reportId>plmkjh8765FGH4rt6As</msg:reportId>

      ‘@ | Set-Content $log -Encoding UTF8

      $requestId = @{
      Count = 1
      Matches = @()
      }

      $repid= [regex]::matches((Get-Content $log),’\w{6}-\w{6}-\w{6}’).value
      foreach( $rep in $repid ) {
      $matched = @($_ | Select-String -Pattern $rep)
      if( -not $matched ) {
      return
      }
      $_
      }
      Get-Content $log

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