28

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).

5 Answers 5

57

Here's one method:

$FileContent = Get-Content "YourFile.txt"
$Matches = Select-String -InputObject $FileContent -Pattern "/export" -AllMatches

$Matches.Matches.Count
Sign up to request clarification or add additional context in comments.

6 Comments

one liner : (Select-String -Path "YourFile.txt" -Pattern "/export" -AllMatches).Matches.Count
Gives "Out of Memory" on very large files, might need to use a streaming technique if working with huge files.
@NealWalters Do you have any more information on a streaming technique? I need to count occurrences of a string in a 6GB file: I expect about 5 million occurrences.
@BennettMcElwee - I can't remember now if I solved it or not.
@NealWalters I'll take that as a no :-) But thanks - I used your idea to come up with a solution that solved my problem.
|
14

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.

4 Comments

This returns the number of lines containing the string, not the total number of matches.
Can confirm what @AllenZ. says
Agreed. I've added a note on it to the answer. Decided not to delete my answer, as it's often a useful approach.
Please at lease put in bold "the count won't be correct"
8

If you're searching in a large file (several gigabytes) that could have have millions of matches, you might run into memory problems. You can do something like this (inspired by a suggestion from NealWalters):

Select-String -Path YourFile.txt -Pattern '/export' -SimpleMatch | Measure-Object -Line

This is not perfect because

  • it counts the number of lines that contain the match, not the total number of matches.
  • it prints some headings along with the count, rather than putting just the count into a variable.

You can probably solve these if you need to. But at least you won't run out of memory.

Comments

0

The best solution I applied was

$count = (Select-String -Path "filepath" -Pattern "pattern" -AllMatches).Matches.Count

This one is memory optimized and accurate

Comments

-4
grep -co vs grep -c

Both are useful and thanks for the "o" version. New one to me.

3 Comments

grep is not a thing in PowerShell.
@Phil It actually is, and very easily to get. Just install the Scoop package manager with irm get.scoop.sh | iex (scoop.sh) then simply install grep scoop install grep. All Unix/GNU utils are perfectly available and functional on Powershell.
my experience with powershell is use when you have to use whatever comes with windows. if you can install arbitrary packages, you can use something else entirely. certainly how to do x in y is not satisfactorily answered by install z

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.