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!

13 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.

  5. on 19 Jul 2020 at 1:05 pm สล็อตXo

    Thanks again for the article.Really thank you! Awesome.

  6. on 19 Jul 2020 at 1:15 pm sun lamp for plant

    Muchos Gracias for your blog article.Much thanks again. Will read on

  7. on 06 Aug 2020 at 11:26 am ABW-001

    It as essentially a cool and beneficial piece of information. I am content which you just shared this valuable data with us. Please hold us informed like this. Thank you for sharing.

  8. on 10 Aug 2020 at 5:52 am mold remediation Rockford

    You, my friend, ROCK! I found just the information I already searched all over the place and just could not locate it. What a perfect website.

  9. on 24 Aug 2020 at 8:36 am Satta king fast

    Im thankful for the article post.Much thanks again.

  10. on 20 Sep 2020 at 7:30 pm Joe Biden T Shirt

    Vitamin E is another treatment that is best

  11. on 28 Dec 2020 at 2:53 pm https://ggwpstore.id/

    ggwpstore adalah sbuah agen yang sangat lengkap aman dan terpercaya
    di indonesia, tidak sama dari agen – agen yang lainnya, ggwpstore tawarkan kemenangan yang amat gampang dan layanan yang sangat ramah, dii percaya sebagai agen pkv games sejak
    2015 sampai kala ini pengguna / user terbukti amat meningkat dari th.
    ke tahun, Artinya makin banyak pengguna spesifik nya orang indonesia pakai jawsa
    sarana agen ggwpstore.

  12. on 11 Jan 2021 at 5:38 am dewatogel

    Your article is a refreshing change from the content I ave been reading on this topic. I agree with a lot of what you are saying here.

  13. on 13 Jan 2021 at 4:48 pm NEYMAR

    It’s through adversity that you find the strength you never knew you had

Did I get this wrong? Let me know!

Trackback URI | Comments RSS

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