Oneliner: Count How Many Times a String Appears in a File

When working with text files, it is usefull to know how to count how many times a string appears in a file. To do that, use this oneliner:

$ grep -o string /path/to/file | wc -l

$ cat jedi
jedi jedi jedi jedi jedi
$ grep -o jedi /path/to/file | wc -l
5

grep -o jedi matches only the jedi strings and displays one match per line. wc -l counts the lines only.

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!