How can I count the occurrences of a string within a file?
BashSearchBash Problem Overview
Just take this code as an example. Pretending it is an HTML/text file, if I would like to know the total number of times that echo
appears, how can I do it using bash?
new_user()
{
echo "Preparing to add a new user..."
sleep 2
adduser # run the adduser program
}
echo "1. Add user"
echo "2. Exit"
echo "Enter your choice: "
read choice
case $choice in
1) new_user # call the new_user() function
;;
*) exit
;;
esac
Bash Solutions
Solution 1 - Bash
The number of string occurrences (not lines) can be obtained using grep
with -o
option and wc
(word count):
$ echo "echo 1234 echo" | grep -o echo
echo
echo
$ echo "echo 1234 echo" | grep -o echo | wc -l
2
So the full solution for your problem would look like this:
$ grep -o "echo" FILE | wc -l
Solution 2 - Bash
This will output the number of lines that contain your search string.
grep -c "echo" FILE
This won't, however, count the number of occurrences in the file (ie, if you have echo multiple times on one line).
edit:
After playing around a bit, you could get the number of occurrences using this dirty little bit of code:
sed 's/echo/echo\n/g' FILE | grep -c "echo"
This basically adds a newline following every instance of echo so they're each on their own line, allowing grep to count those lines. You can refine the regex if you only want the word "echo", as opposed to "echoing", for example.
Solution 3 - Bash
I'm taking some guesses here, because I don't quite understand what you're asking.
I think that what you want is a count of the number of lines on which the pattern 'echo' appears in the given file.
I've pasted your sample text into a file called 6741967
.
First, grep
finds the matches:
james@Brindle:tmp$grep echo 6741967
echo "Preparing to add a new user..."
echo "1. Add user"
echo "2. Exit"
echo "Enter your choice: "
Second, use wc -l
to count the lines
james@Brindle:tmp$grep echo 6741967 | wc -l
4
Solution 4 - Bash
None of the existing answers worked for me with a single-line 10GB file. Grep runs out of memory even on a machine with 768 GB of RAM!
$ cat /proc/meminfo | grep MemTotal
MemTotal: 791236260 kB
$ ls -lh test.json
-rw-r--r-- 1 me all 9.2G Nov 18 15:54 test.json
$ grep -o '0,0,0,0,0,0,0,0,' test.json | wc -l
grep: memory exhausted
0
So I wrote a very simple Rust program to do it.
- Install Rust.
cargo install count_occurences
$ count_occurences '0,0,0,0,0,0,0,0,' test.json
99094198
It's a little slow (1 minute for 10GB), but at least it doesn't run out of memory!
Solution 5 - Bash
if you just want the number of occurences then you can do this, $ grep -c "string_to_count" file_name