Posted almost 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:

Copy
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:

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

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

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