forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrep.ps1
163 lines (139 loc) · 5.88 KB
/
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
<#
.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/grep.ps1" -outFile "$pwd\grep.ps1";
.\grep.ps1 [-path] [-pattern]
.EXAMPLE
example to search clustermanifest.xml files for old thumbprint and replace with new thumbprint
.\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'
)
$global:matchedFiles = @{}
function main() {
$error.clear()
if ($matchLine) {
$pattern = ".*$pattern.*"
}
$startTime = get-date
$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-host "checking $totalFiles files"
foreach ($file in $files) {
$fileCount++
$sr = $null
write-host "checking $fileCount of $totalFiles $file" -ForegroundColor DarkGray
if ([io.path]::GetExtension($file) -ieq $backupExtension) {
write-host "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 }
if ($regex.IsMatch($content)) {
write-host $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 }
$matchCount++
$matchObj = @{
line = $line
index = $match.index
length = $match.Length
value = $match.value
}
[void]$global:matchedFiles.$file.add($matchObj)
write-host " $($line):$($match | Select-Object index, length, value)"
if ($null -ne $replace) {
$newLine = $regex.Replace($content, $replace)
write-host "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-host "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-host "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-host "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-host "matched files summary:" -ForegroundColor Green
foreach ($m in $global:matchedFiles.getenumerator()) {
write-host "`t$($m.key) matches:$($m.value.count)" -ForegroundColor Cyan
}
write-host
write-host "$($global:matchedFiles.Count) matched files in global variable: `$global:matchedFiles" -ForegroundColor Magenta
write-host "to view: `$global:matchedFiles | convertto-json" -ForegroundColor Magenta
}
else {
write-host "0 matched files" -ForegroundColor Yellow
}
write-host "finished: total files:$($filecount) total matched files:$($global:matchedFiles.count) total matches:$($matchCount) total minutes:$((get-date).Subtract($startTime).TotalMinutes)" -ForegroundColor Magenta
}
main