-
Notifications
You must be signed in to change notification settings - Fork 17
/
ps-grep.ps1
203 lines (173 loc) · 7.46 KB
/
ps-grep.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<#
.SYNOPSIS
powershell script to search (grep) files in given path for regex pattern and optionally replace with new string.
.LINK
invoke-webRequest "https://raw.githubusercontent.com/jagilber/powershellScripts/master/ps-grep.ps1" -outFile "$pwd\ps-grep.ps1";
.\ps-grep.ps1 [-path] [-pattern]
.EXAMPLE
example to search clustermanifest.xml files for old thumbprint and replace with new thumbprint
.\ps-grep.ps1 -pattern '%old thumbprint%' `
-path 'd:\svcfab' `
-filePattern 'clustermanifest.*.xml' `
-includeSubDirs `
-replace '%new thumbprint%' `
-createBackup `
-whatIf
#>
[cmdletbinding()]
param(
[string]$pattern = '.*',
[string]$path = $pwd,
[string]$filePattern = '.*',
[switch]$includeSubDirs,
[object]$replace = $null,
[switch]$createBackup,
[switch]$matchLine,
[switch]$whatIf,
[string]$backupExtension = '.bak',
[switch]$quiet
)
$global:matchedFiles = @{}
function main() {
$startTime = get-date
$error.clear()
if ($matchLine) {
$pattern = ".*$pattern.*"
}
if ($filePattern.startsWith('*')) {
# $filePattern = [regex]::escape($filePattern)
$filePattern = '.' + $filePattern
write-console "escaped filePattern: $filePattern"
}
$fileCount = 0
$regex = [regex]::new($pattern, [text.regularexpressions.regexoptions]::Compiled -bor [text.regularexpressions.regexoptions]::IgnoreCase)
$files = @(@(get-childitem -recurse:$includeSubDirs -file -path $path) | Where-Object Name -match $filePattern).FullName
write-verbose "filtered files: $($files | Format-List * | out-string)"
$totalFiles = $files.count
write-console "checking $totalFiles files"
foreach ($file in $files) {
$fileCount++
$sr = $null
write-console "checking $fileCount of $totalFiles $file" -ForegroundColor DarkGray
if ([io.path]::GetExtension($file) -ieq $backupExtension) {
write-console "skipping backup file $file" -ForegroundColor Yellow
continue
}
try {
$line = 0
$error.clear()
$sr = [io.streamreader]::new($file)
$content = $sr.readtoend()
if ($content.Length -lt 1) { continue }
# read first 100 bytes or max length if less than 100 bytes to check if file is binary or text, then reset position
$testContent = $content.substring(0, [math]::min(100, $content.Length))
$bytes = [system.text.encoding]::UTF8.GetBytes($testContent)
$isBinary = $false
foreach ($byte in $bytes) {
if ($byte -lt 32 -and $byte -ne 9 -and $byte -ne 10 -and $byte -ne 13) {
write-console "skipping binary file $file" -ForegroundColor Yellow
$isBinary = $true
break
}
}
if ($isBinary) { continue }
$sr.basestream.position = 0
if ($regex.IsMatch($content)) {
write-console $file -ForegroundColor Magenta
[void]$global:matchedFiles.Add($file, [collections.arraylist]::new())
}
else {
continue
}
$sr.basestream.position = 0
[text.stringbuilder]$replaceContent = [text.stringbuilder]::new()
while ($null -ne ($content = $sr.ReadLine())) {
$line++
if ($content.Length -lt 1) { continue }
if ($regex.IsMatch($content)) {
$matches = $regex.Matches($content)
foreach ($match in $matches) {
if ($match.Length -lt 1) { continue }
$groupsTable = @{}
foreach ($g in $match.groups) {
[void]$groupsTable.add($g.name, $g.value)
}
$matchCount++
$matchObj = [ordered]@{
file = $file
line = $line
index = $match.index
length = $match.Length
value = $match.value
#captures = $match.captures
groups = $groupsTable
}
[void]$global:matchedFiles.$file.add($matchObj)
write-console " $($line):$($match | Select-Object index, length, value)"
if ($null -ne $replace) {
$newLine = $regex.Replace($content, $replace)
write-console "replacing line:$($line) match:'$($match.value)' with '$replace'`n`toldLine:'$content'`n`tnewLine:'$newLine'" -ForegroundColor Cyan
[void]$replaceContent.AppendLine($newLine)
}
}
}
elseif ($null -ne $replace) {
[void]$replaceContent.AppendLine($content)
}
}
if ($null -ne $replace) {
if ($sr -ne $null) {
$sr.close()
$sr.dispose()
}
if ($createBackup) {
write-console "saving backup file $file$backupExtension" -ForegroundColor Green
if (!$whatIf) {
[io.file]::copy($file, "$file$backupExtension", $true)
}
}
# remove readonly if set
$att = [io.file]::GetAttributes($file)
if ($att -band [io.fileAttributes]::ReadOnly) {
write-console "attempting to remove readonly attribute from file $file" -ForegroundColor Yellow
$att = $att -band (-bnot [io.fileAttributes]::ReadOnly)
if (!$whatIf) {
[io.file]::SetAttributes($file, $att)
}
}
write-console "saving replacements in file $file" -ForegroundColor Green
if (!$whatIf) {
[io.file]::WriteAllText($file, $replaceContent.ToString())
}
}
}
finally {
if ($null -ne $sr) {
$sr.close()
$sr.dispose()
}
}
}
if ($global:matchedFiles.Count) {
write-console "matched files summary:" -ForegroundColor Green
foreach ($m in $global:matchedFiles.getenumerator()) {
write-console "`t$($m.key) matches:$($m.value.count)" -ForegroundColor Cyan
}
write-console
write-console "$($global:matchedFiles.Count) matched files in global variable: `$global:matchedFiles" -ForegroundColor Magenta
write-console "to view: `$global:matchedFiles | convertto-json" -ForegroundColor Magenta
}
else {
write-console "0 matched files" -ForegroundColor Yellow
}
write-console "finished: total files:$($filecount) total matched files:$($global:matchedFiles.count) total matches:$($matchCount) total minutes:$((get-date).Subtract($startTime).TotalMinutes)" -ForegroundColor Magenta
}
function write-console([object]$msg, [consolecolor]$foregroundColor = [consolecolor]::White) {
if ($quiet) {
Write-Verbose ($msg | Out-String)
}
else {
write-host $msg -ForegroundColor $foregroundColor
}
}
main