forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-unique-lines.ps1
100 lines (81 loc) · 2.47 KB
/
find-unique-lines.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
powershell find unique lines in file
.DESCRIPTION
powershell script to find unique lines in file
.NOTES
File Name : find-unique-lines.ps1
Author : jagilber
Version : 150414
.EXAMPLE
.\find-unique-lines.ps1 -file c:\temp\test1.txt
.PARAMETER file
the text file for compare
#>
Param(
[parameter(Position=0,Mandatory=$true,HelpMessage="Enter path to first file")]
[string] $file,
[parameter(Position=1,Mandatory=$false,HelpMessage="Enter regex")]
[string] $regex
)
cls
$count = 0
$lineList = @{}
$logfile = "find-unique-lines.ps1.txt"
# ----------------------------------------------------------------------------------------------------------------
function main()
{
if([IO.File]::Exists($file))
{
[IO.StreamReader] $reader = new-object IO.StreamReader ($file)
while ($reader.Peek() -ge 0)
{
$line = $reader.ReadLine()
if(![string]::IsNullOrEmpty($regex))
{
If([regex]::IsMatch($line, $regex, [Text.RegularExpressions.RegexOptions]::IgnoreCase))
{
$line = [regex]::Match($line, $regex,[Text.RegularExpressions.RegexOptions]::IgnoreCase)
}
else
{
continue
}
}
$count++
if(!$lineList.ContainsKey($line))
{
$lineList.Add($line,1)
}
else
{
$oldvalue = $lineList[$line]
$linelist.Remove($line)
$lineList.Add($line,++$oldvalue)
}
}
}
else
{
log-info "file $($file) does not exist. exiting..."
return
}
foreach($kvp in ($lineList.GetEnumerator() | sort Value))
#foreach($kvp in $lineList)
{
log-info "$($kvp.Value):$($kvp.key)"
}
log-info "------------------------------------------------"
log-info "Total Unique lines:$($lineList.Count)"
log-info "Total lines:$($count)"
log-info "finished"
}
# ----------------------------------------------------------------------------------------------------------------
function log-info($data)
{
# $data = "$([DateTime]::Now):$($data)`n"
Write-Host $data
# out-file -Append -InputObject $data -FilePath $logFile
}
# ----------------------------------------------------------------------------------------------------------------
main