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:
<requestId>qwerty-qwer12-qwer56</requestId>Ace of Base Order: Q2we45-Uj87f6-gh65De<something else...
<requestId>zxcvbn-zxcv4d-zxcv56</requestId>
<requestId>1234qw-12qw9x-123456</requestId> Stevie Wonder <messageId>1234qw-12qw9x-123456</msg
Desired output:
<requestId>Request-1</requestId>Ace of Base Order: Request-2<something else...
<requestId>Request-3</requestId>
<requestId>Request-4</requestId> Stevie Wonder <messageId>Request-4</msg
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”)?
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”)?
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.
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:
<requestId>RequestId-1</requestId>Ace of Base Order: RequestId-2<something else...
<requestId>RequestId-3</requestId>
<requestId>RequestId-4</requestId> Stevie Wonder <messageId>RequestId-4</msg
<reportId>ReportId-1</msg:reportId>something here.,. <requestId>RequestId-3</requestId>
reportId>ReportId-2</msg:reportId> uraaa 123 <keyID>KeyId-1</msgdc
Do you have an idea how to achieve that? Many thanks in advance!
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!
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:
@'
<requestId>qwerty-qwer12-qwer56</requestId>Ace of Base Order: Q2we45-Uj87f6-gh65De<something else...
<requestId>zxcvbn-zxcv12-zxcv56</requestId>
<requestId>1234qw-12qw12-123456</requestId> Stevie Wonder <messageId>1234qw-12qw12-123456</msg
<reportId>plmkjh8765FGH4rt6As</msg:reportId>something here.,. <requestId>zxcvbn-zxcv12-zxcv56</requestId>
<reportId>poGd56Hnm9q3Dfer6Jh</msg:reportId> uraaa 123 <keyID>poU6Ghk89edfTG78Jk45GrRt23HzW4pl</msgdc
Abcd <requestId>1234qw-12qw12-123456</requestId> abcdef ole ole Order: zxcvbn-zxcv12-zxcv56 abracadabra <keyID>poU6Ghk89edfTG78Jk45GrRt23HzW4pl</msgdc
User_2
User-2
User_1
User_12
User-12
User 12
User 9
'@
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):