Skip to content

Instantly share code, notes, and snippets.

@midnightfreddie
Last active February 18, 2016 05:46
Show Gist options
  • Save midnightfreddie/67e68d9cb984b3ad6e89 to your computer and use it in GitHub Desktop.
Save midnightfreddie/67e68d9cb984b3ad6e89 to your computer and use it in GitHub Desktop.
# In reply to https://www.reddit.com/r/PowerShell/comments/46bwtd/help_with_counting_in_csv_files/
# Tested against test csvs generated by the other script in this gist, and it worked without modification!
param (
$Path = ".",
$OutFile = "C:\temp\countlines.csv"
)
Get-ChildItem -Filter *.csv -Path $Path -Recurse |
ForEach-Object {
$File = $_
# Blank the Sums hash
$Sums = @{}
# Count each Hour value by using it as the hash key and adding to the value
# New to me: +=1 to a nonexistent key makes that key value 1 ... how convenient!
Import-Csv -Path $File.FullName |
ForEach-Object {
$Sums[($_.Hour)] += 1
}
$Sums.Keys | ForEach-Object {
$Hour = $_
$Count = $Sums[$_]
New-Object psobject -Property ([ordered]@{
Name = $File.name
Hour = $Hour
Count = $Count
SecretSauce = ($Count * $Hour * 60) / (20106.19298 * $Hour * 60) * ([math]::Exp($Hour/123))
})
}
} |
Export-Csv -NoTypeInformation -Path $OutFile
$NumCsvs = 5
$NumRows = 100
$Hours = -120..0
1..$NumCsvs | ForEach-Object {
1..$NumRows | ForEach-Object {
New-Object psobject -Property @{ Hour = Get-Random $Hours }
} |
Export-Csv -NoTypeInformation -Path "$_.csv"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment