Feed on

let’s say you’ve got a string and you’d like to count the number of times some substring appears in it. let’s also assume that you’re lazy and you don’t want to leave your shell terminal to do it.  fortunately, there’s an easy way to count string matches from the command-line using a perl one-liner.   let’s say we want to count the number of commas in the text file my_file.txt. all you need to write is: 

$ perl -nle ‘print tr/,/,/;’ < my_file.txt 

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!

4 Responses to “use a perl one-liner to count the number of matches on the command-line”

  1. on 28 May 2010 at 2:59 pm Brian

    This is great for a single-line file. Thanks.

    To get total matches on a multi-line file I ended up doing

    perl -lne ‘$a += tr/,//; END {print $a}’ my_file.txt

  2. on 11 Jun 2020 at 5:56 am suba suba

    wish for enjoyment, since this this web site conations really nice funny information too.

  3. on 29 Jun 2020 at 7:46 am Reusable Face Mask Manufacturer UK

    This particular blog is without a doubt cool and also informative. I have picked up a lot of handy tips out of it. I ad love to go back again and again. Thanks a bunch!

  4. on 08 Jul 2020 at 12:35 pm Rolet online

    Si vous etes interesse, faites le pas et contactez un des mediums qui fait partie de notre centre d aastrologie et laissez-vous predire votre futur.

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

More blogs about http://desk.stinkpot.org:8080/tricks.