DEV Community

raghuncs
raghuncs

Posted on

1 1

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay