Posted over 7 years ago. Visible to the public.

Grep the number of occurences in a file, counting multiple hits per line

Grep prints one line per match. To return the number if matches, use the -c switch:

grep -c "something" filename

However, if a word appears more than once in a line, it is only counted once.

To count every match, you can use sed to force line breaks on multiple matches:

sed 's/something/something\n/g' filename | grep -c "something"

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Arne Hartherz
Last edit:
11 months ago
by Arne Hartherz
linux, unix, bash, shell
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more