Reputation: 31
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.
Upvotes: 3
Views: 2001
Reputation: 5227
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:
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.
Upvotes: 3
Reputation: 14705
Same as @robdy answer, but without the global
scope refference:
# Simulation of Get-Content
$content = "aXYZ","bXYZ","cXYZ","dXYZ","eXYZ"
$i = 0
$result = $content.ForEach({
$i++
$_ -replace 'XYZ', $i
})
$result | Out-File -FilePath "$env:TEMP\test.txt" -Encoding utf8 -NoClobber
We're basically looping over the content of the file and replace the string XYZ
with the number. Then we save that in to a new variable, which is later used to write the new content to the file.
Upvotes: 1