I have a TXT containing 350.000 URL paths. I want to count the distinct occurrences of a string that is included in the path. I don't know in advance the strings. I just know that it comes in 2nd place of the path.
The paths looks like this:
images/STRINGA/2XXXXXXXX_rTTTTw_GGGG1_Top_MMM1_YY02_ZZZ30_AAAA5.jpg images/STRINGA/3XXXXXXXX_rTTTTw_GGGG1_Top_MMM1_YY02_ZZZ30_AAAA5.jpg images/STRINGB/4XXXXXXXX_rTTTTw_GGGG1_Top_MMM1_YY02_ZZZ30_AAAA5.jpg images/STRINGB/5XXXXXXXX_rTTTTw_GGGG1_Top_MMM1_YY02_ZZZ30_AAAA5.jpg images/STRINGC/5XXXXXXXX_rTTTTw_GGGG1_Top_MMM1_YY02_ZZZ30_AAAA5.jpg
So I split the path with $searchstring= $item.Split("/")[1]. Now I can iterate through all the lines in the TXT extracting the string. But how can I make a final document looking like this: STRINGA;2 STRINGB;2 STRINGC;1
How can I dynamically extract a string and then search its occurrences? I need just a hint, I can get the rest working then.
Use the Group-Object
command:
Get-Content path\to\urls.txt |Group-Object { $_.Split('/')[1] } -NoElement |Select-Object @{Name='String';Expression='Name'},Count |Export-Csv path\to\output.csv -Delimiter ';' -NoTypeInformation