Posted about 8 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"

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Arne Hartherz
Last edit:
over 1 year 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