Feed on
Posts
Comments

to count the number of times you match a substring in a larger text file, you can use the following shell one-liner:

>> sed “s/[sub_str]/[sub_str]\n/g” [text_file] | wc -l


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

2 Responses to “count matches on the command-line”

  1. on 23 Feb 2012 at 6:10 am zoltanctoth

    Brilliant idea! :)

  2. on 16 Jul 2012 at 6:39 am Rob Stewart

    Why not just use grep -c ‘sub_str’ text_file?

Did I get this wrong? Let me know!

Trackback URI | Comments RSS