-
Notifications
You must be signed in to change notification settings - Fork 1
/
CountLabels.ps1
65 lines (54 loc) · 2.1 KB
/
CountLabels.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
# Prompt the user to enter the path to the directory containing the .txt files
$directoryPath = Read-Host -Prompt "Enter the path to the directory containing the .txt files"
# Check if the directory exists
if (-Not (Test-Path -Path $directoryPath)) {
Write-Output "The directory path '$directoryPath' does not exist. Please check the path and try again."
exit
}
# Initialize a hashtable for the overall count across all files
$overallCount = @{}
# Get all .txt files in the directory
$files = Get-ChildItem -Path $directoryPath -Filter *.txt
# Check if there are any .txt files in the directory
if ($files.Count -eq 0) {
Write-Output "No .txt files found in the directory '$directoryPath'."
exit
}
# Loop through each file
foreach ($file in $files) {
# Initialize a hashtable for the individual file count
$fileCount = @{}
# Read the content of the file
$lines = Get-Content -Path $file.FullName
# Process each line in the file
foreach ($line in $lines) {
# Extract the first column (split by space and take the first element)
$number = ($line -split '\s+' | Select-Object -First 1) -as [int]
# Ensure $number is a valid integer
if ($number -ne $null) {
# Update the count for the current file
if ($fileCount.ContainsKey($number)) {
$fileCount[$number]++
} else {
$fileCount[$number] = 1
}
# Update the overall count
if ($overallCount.ContainsKey($number)) {
$overallCount[$number]++
} else {
$overallCount[$number] = 1
}
}
}
# Output the count for the current file with sorted keys
Write-Output "Counts for file $($file.Name):"
foreach ($key in ($fileCount.Keys | Sort-Object)) {
Write-Output "$($key+1) : $($fileCount[$key])"
}
Write-Output ""
}
# Output the overall count across all files with sorted keys
Write-Output "Overall Counts across all files:"
foreach ($key in ($overallCount.Keys | Sort-Object)) {
Write-Output "$($key+1) : $($overallCount[$key])"
}