Is it possible to count specific strings in a file and save the value in a variable?
For me it would be the String "/export" (without quotes).
Join Stack Overflow to learn, share knowledge, and build your career.
Is it possible to count specific strings in a file and save the value in a variable?
For me it would be the String "/export" (without quotes).
Here's one method:
$FileContent = Get-Content "YourFile.txt"
$Matches = Select-String -InputObject $FileContent -Pattern "/export" -AllMatches
$Matches.Matches.Count
(Select-String -Path "YourFile.txt" -Pattern "/export" -AllMatches).Matches.Count
– akhansari
Apr 21 '20 at 15:33
Here's a way to do it.
$count = (get-content file1.txt | select-string -pattern "/export").length
As mentioned in comments, this will return the count of lines containing the pattern, so if any line has more than one instance of the pattern, the count won't be correct.
grep -co vs grep -c
Both are useful and thanks for the "o" version. New one to me.