DEV Community

raghuncs
raghuncs

Posted on

Matching every instance of a pattern in UNIX

For example I have a string "raghuraghuraghuhello" and I want to display the number of times 'raghu' has occurred in the string.

We can use the below variant of grep

echo raghuraghuraghuhello|grep -o raghu|wc -l

hadoop@ubuntu:~$ echo raghuraghuraghuhello|grep -o raghu
raghu
raghu
raghu
hadoop@ubuntu:~$ echo raghuraghuraghuhello|grep -o raghu|wc -l
3

Note: When in the first command it just printed out the instances of the pattern, hence we piped it to wc -l

Top comments (0)