Reputation: 859
I got a text file with the following layout,
Lorem Ipsum Lorem Ipsum Ipsum user:john
Lorem Ipsum user:peter
Lorem Ipsum Lorem Ipsum user:george
Lorem Ipsum user:john
Lorem Ipsum vLorem Ipsum user:george
Lorem Ipsum user:john
I must develop a script on Powershell V2 to count the occurrences and build a CSV with the following content,
john,3
george,2
peter,1
I was planning to loop thru the file saving each user in an array and then use get-content and a pattern to count the ocurrences, for example:
#assumming i was able to fill the array in some way :)
$users =@('john','peter', 'george')
for each ($user in $users)
{
$count = get-content .\myfile.txt | select-string -pattern "user:$user"
write-host $count
}
#save the CSV
Does it makes sense? I'm ear-opened to your hints and tips. Knowing the power of Powershell I'm pretty user there is a better approach. Thanks!
Upvotes: 3
Views: 13825
Reputation: 126732
Here's another option using the Group-Object
cmdlet:
Get-Content lorem.txt |
Foreach-Object {$_ -replace '^.+user:(.+)$','$1' } |
Group-Object -NoElement
Upvotes: 0
Reputation: 16792
With your current approach you will be reading the file from disk once for every user. It might be better to scan the file once, and collect all users in a single pass.
It sounds like you don't have the list of users ahead of time, you basically need to scan for strings like user:<username here>
and keep a running tally of the different usernames you find.
Here's a function that should do the basic job:
function GetUserCounts($fileName)
{
$userCounts = @{}
switch -regex -file $fileName
{
'\buser:([a-zA-Z]+)\b' {
$userName = $matches[1]
$userCounts[$userName] = [int]$userCounts[$userName] + 1
}
}
$userCounts.GetEnumerator() | select Name,Value
}
That Then you can create a CSV like this:
PS> GetUserCounts .\myfile.txt | Export-Csv .\counts.csv
Upvotes: 3