forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-RegexReplace.ps1
98 lines (88 loc) · 2.83 KB
/
Set-RegexReplace.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<#
.SYNOPSIS
Updates text found with Select-String, using a regular expression replacement template.
.INPUTS
Microsoft.PowerShell.Commands.MatchInfo containing a regex match than will be replaced.
.OUTPUTS
System.String of the input string if the Select-String's input was a string instead of a file.
(File changes will be saved back to the file.)
.LINK
https://docs.microsoft.com/dotnet/standard/base-types/substitutions-in-regular-expressions
.LINK
https://github.com/brianary/Detextive/
.EXAMPLE
Select-String '(<!-- generated) .*? (-->)' README.md |Set-RegexReplace.ps1 "`$1 $(Get-Date) `$2"
Updates the generated date in README.md.
.EXAMPLE
Get-Item README.md |select Name,Length |ConvertTo-Html -Fragment |Out-String |Select-String '(<td)(>\d+</td>)' |Set-RegexReplace.ps1 '$1 align="right"$2'
<table>
<colgroup><col/><col/></colgroup>
<tr><th>Name</th><th>Length</th></tr>
<tr><td>README.md</td><td align="right">21099</td></tr>
</table>
#>
#Requires -Version 3
#Requires -Modules Detextive
[CmdletBinding()] Param(
<#
A regular expression replacement string.
* $$ is a literal $
* $_ is the entire input string.
* $` is the string before the match.
* $& is the entire matching string.
* $' is the string after the match.
* $1 is the first matching group.
* $n (where n is a number) is the nth matching group.
* $name is the group named "name".
* $+ is the last matching group.
#>
[Parameter(Position=0,Mandatory=$true)][string] $Replacement,
# The output from Select-String.
[Parameter(ValueFromPipeline=$true)][Microsoft.PowerShell.Commands.MatchInfo] $InputObject
)
Begin
{
Write-Verbose 'Begin'
$files = @{}
}
Process
{
Write-Verbose 'Process'
if($InputObject.Path -eq 'InputStream')
{
Write-Verbose "Line: $($InputObject.Line)"
if($null -eq $InputObject.Context)
{
return ($InputObject.Line -replace $InputObject.Pattern,$Replacement)
}
else
{
return ($InputObject.Context.PreContext,
($InputObject.Line -replace $InputObject.Pattern,$Replacement),
$InputObject.Context.PostContext |Out-String)
}
}
elseif(!$files.ContainsKey($InputObject.Path))
{
Write-Verbose "Adding '$($InputObject.Path)'"
$files.Add($InputObject.Path,$InputObject.Pattern)
}
else
{
Write-Verbose "Already added '$($InputObject.Path)'"
}
}
End
{
Write-Verbose 'End'
$i,$max = 0,($files.Count/100)
Write-Verbose "Updating $($files.Count) files"
foreach($file in $files.Keys)
{
$pattern = $files[$file]
Write-Progress 'Performing file replace' "$pattern" -curr $file -percent ($i++/$max)
Write-Verbose "$($InputObject.Path) : -replace '$($InputObject.Pattern)','$Replacement'"
(Get-Content $file -Raw) -replace $pattern,$Replacement |Out-File $file (Get-FileEncoding $file)
}
Write-Progress 'Performing file replace' 'Complete' -Completed
}