Matching every instance of a pattern in UNIX

raghuncs

raghuncs

Posted on March 16, 2020

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

💖 💪 🙅 🚩
raghuncs
raghuncs

Posted on March 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related