Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace characters with incrementing value

Tags:

powershell

I'm trying to figure out how to replace a certain string that repeats itself multiple time in a txt file with an incrementing value.

What I try to achieve is the following:

I have multiple HTML links in a table, each of the links have the exact same ending i.e.: XYZ

I want to change XYZ to 1, 2, 3, 4, 5 and so on... using PowerShell.

(Get-Content c:\temp\test.txt).replace('XYZ', 'MyValue') |
    Set-Content c:\temp\test.txt

It is the 'MyValue' part of using an incrementing number for each replacement I'm not sure how to achieve it.

like image 722
Alex Pelletier Avatar asked Jul 22 '26 08:07

Alex Pelletier


1 Answers

Hint: be aware of using Get-Content | .... | Set-Content to the same file due to possible data loss! It doesn't apply to reading file in a subexpression/grouping expression as you did, but be aware of it in the future (as a best practice). Credits to @Ansgar Wiechers for clarification.


Solution:

You can define callback function to [Regex]::Replace (String, String, MatchEvaluator) method:

$global:counter = 0
$content = (Get-Content c:\temp\test.txt)

# Below can be used to verify it's working correctly instead of creating a file
# $content = "aXYZ","bXYZ","cXYZ","dXYZ","eXYZ"

$content | % {[Regex]::Replace($_, 'XYZ', {return $global:counter += 1}) } | Set-Content c:\temp\test.txt

Output will be:

a1
b2
c3
d4
e5

Explanation:

The parameters you pass to Replace are as follows:

  1. The string to search for a match.
  2. The regular expression pattern to match.
  3. A custom method that examines each match and returns either the original matched string or a replacement string.

The custom method increments the counter each time the regex is matched. Unfortunately, for some reason, it requires you to use global variable scope which is in general not the best practice if not needed (as mentioned by @Paxz in the comments).

As @TessellatingHeckler found out, the first version of solution used Replace on $content variable which resulted in removing all newlines. You have to use Foreach-Object (or % as its alias) to do the replacing in each single line.

like image 106
Robert Dyjas Avatar answered Jul 28 '26 12:07

Robert Dyjas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!