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

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

Tagged with: , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

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




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