-
Notifications
You must be signed in to change notification settings - Fork 17
/
file-regex-string-compare.ps1
154 lines (127 loc) · 4.62 KB
/
file-regex-string-compare.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
<#
.SYNOPSIS
powershell script to compare regex pattern match strings in two files
.DESCRIPTION
powershell script to compare regex pattern match strings in two files
.NOTES
File Name : regex-file-compare.ps1
Author : jagilber
Version : 140926
.EXAMPLE
.\logmanwrapper.ps1 -regexpattern "KB[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" -fileOne c:\temp\test1.txt -fileTwo c:\temp\test2.txt
deploy all configuration files in default 'configs' or 'configs_templates' folder to local machine using defalut etl output folder of %systemroot%\temp
.PARAMETER regexpattern
regex pattern to find string matches for compare between two files
.PARAMETER fileOne
the first text file for compare
.PARAMETER fileTwo
the second text file for compare
#>
Param(
[parameter(Position=0,Mandatory=$true,HelpMessage="Enter the quoted regex pattern")]
[string] $regexpattern,
[parameter(Position=1,Mandatory=$true,HelpMessage="Enter path to first file")]
[string] $fileOne,
[parameter(Position=2,Mandatory=$true,HelpMessage="Enter path to second file")]
[string] $fileTwo
)
# modify
cls
#$regexPattern = "KB[0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
#$fileOne = "F:\cases\114091611803231\admp4\790bf25f-e78a-4592-9a28-52221c287d16\IBDSADMP4_Hotfixes.TXT"
#$fileTwo = "F:\cases\114091611803231\admp6-working-server\cd3f6d65-2c50-4efe-88ab-9cc549bf39f1\IBDSADMP6_Hotfixes.TXT"
$listOne = @{}
$listTwo = @{}
$listCombined = @{}
$listOneOnly = @{}
$listTwoOnly = @{}
# get all matches from first file
$regex = new-object System.Text.RegularExpressions.Regex($regexPattern,[System.Text.RegularExpressions.RegexOptions]::Singleline)
$matchesOne = $regex.Matches([System.IO.File]::ReadAllText($fileOne))
foreach($match in $matchesOne)
{
$value = $match.Groups[0].Value
if(!$listOne.Contains($value))
{
$value
$listOne.Add($value,$value)
}
}
# get all matches from second file
$matchesTwo = $regex.Matches([System.IO.File]::ReadAllText($fileTwo))
foreach($match in $matchesTwo)
{
$value = $match.Groups[0].Value
if(!$listTwo.Contains($value))
{
$value
$listTwo.Add($value,$value)
}
}
# find all matches in common
foreach($item in $listOne.GetEnumerator())
{
if($listTwo.Contains($item.key))
{
if(!$listCombined.ContainsKey($item.Key))
{
$listCombined.Add($item.key,$item.value)
}
}
else
{
$listOneOnly.Add($item.key,$item.value)
}
}
foreach($item in $listTwo.GetEnumerator())
{
if($listOne.Contains($item.Key))
{
if(!$listCombined.ContainsKey($item.Key))
{
$listCombined.Add($item.Key,$item.Value)
}
}
else
{
$listTwoOnly.Add($item.key,$item.value)
}
}
# list all in common
write-host "*************************************************************"
write-host "Items in both files:$($listCombined.Count)"
write-host "*************************************************************"
foreach($item in $listCombined.GetEnumerator())
{
$item.Key
}
# list differences from first file
write-host "*************************************************************"
write-host "Items only in first file:$($listOneOnly.Count) out of $($listOne.Count)"
write-host "*************************************************************"
foreach($item in $listOneOnly.GetEnumerator())
{
$item.Key
}
# list differences from second file
write-host "*************************************************************"
write-host "Items only in second file:$($listTwoOnly.Count) out of $($listTwo.Count)"
write-host "*************************************************************"
foreach($item in $listTwoOnly.GetEnumerator())
{
$item.Key
}
write-host "*************************************************************"
write-host "Items in both files:$($listCombined.Count)"
write-host "*************************************************************"
# list differences from first file
write-host "*************************************************************"
write-host "Items only in first file:$($listOneOnly.Count) out of $($listOne.Count)"
write-host "*************************************************************"
# list differences from second file
write-host "*************************************************************"
write-host "Items only in second file:$($listTwoOnly.Count) out of $($listTwo.Count)"
write-host "*************************************************************"
write-host "*************************************************************"
write-host "finished"
write-host "*************************************************************"