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

Updated . Posted . Visible to the public.

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 Show archive.org snapshot to force line breaks on multiple matches:

sed 's/something/something\n/g' filename | grep -c "something"
Arne Hartherz
Last edit
Arne Hartherz
Keywords
linux, unix, bash, shell
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2011-08-09 20:25)