If you want to get count of a specific pattern/text in a file, then following Bash Command would solve your problem; let's try to understand this with the help of an example:
Let's say, we want to get count of pattern "Value(0)" in the following file named "Stats"
File Name: Stats
STATS Value(X) Mode 1 STATS Value(X) Mode 3 STATS Value(0) Mode 0 STATS Value(0) Mode 0 STATS Value(X) Mode 11 STATS Value(X) Mode 5 STATS Value(0) Mode 0
The command for performing this task would be:
grep -c "Value(X)" Stats
Displayed result will be: 3
So how this works! We used "GREP" utility with "-c" i-e "grep -c", this basically gives us the count of a pattern/text in any file, like it did in above example, it gave us correct count of pattern "Value(X)" in file named "Stats".
You might have to mention the file path in the command too, if you are not running the script in the same directory where file is placed.
Contact Us for News Tips, Corrections and Feedback
grep -c “Value(X)” returns 4, not 3.
Also, grep -c doesn’t return the count of a specific pattern, it returns the count of the number of lines that contain that pattern. Some lines may contain the pattern more than once, but only the first is counted.