{"id":475859,"date":"2022-07-20T09:02:33","date_gmt":"2022-07-20T06:02:33","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-count-all-matches-of-a-string-with-grep-for-linux\/"},"modified":"2022-07-20T09:02:33","modified_gmt":"2022-07-20T06:02:33","slug":"how-to-count-all-matches-of-a-string-with-grep-for-linux","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-count-all-matches-of-a-string-with-grep-for-linux\/","title":{"rendered":"#How To Count All Matches of a String With grep For Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a27814a2c2c5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a27814a2c2c5\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-count-all-matches-of-a-string-with-grep-for-linux\/#%E2%80%9CHow_To_Count_All_Matches_of_a_String_With_grep_For_Linux%E2%80%9D\" >&#8220;How To Count All Matches of a String With grep For Linux&#8221;<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-count-all-matches-of-a-string-with-grep-for-linux\/#Counting_Matches_With_grep\" >Counting Matches With grep<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-count-all-matches-of-a-string-with-grep-for-linux\/#Counting_Across_Multiple_Files\" >Counting Across Multiple Files<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E2%80%9CHow_To_Count_All_Matches_of_a_String_With_grep_For_Linux%E2%80%9D\"><\/span>&#8220;How To Count All Matches of a String With grep For Linux&#8221;<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<div>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-4038\" data-pagespeed-no-defer=\"\" src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2017\/07\/add8ac45.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Bash Shell\" width=\"1400\" height=\"600\"\/><\/p>\n<p><code>grep<\/code>\u00a0is a text search utility that can work with standard input or multiple files at once. It\u2019s used to print out matches for patterns, strings, or regular expressions. It\u2019s often useful to be able to count the number of matches, which <code>grep<\/code> can do pretty easily.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Counting_Matches_With_grep\"><\/span>Counting Matches With grep<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>grep<\/code>\u00a0command has the <strong><code>-c<\/code>\u00a0flag<\/strong>, which will count the number of lines matched and print out a number. This is useful for lots of things, such as searching through log files for the number of entries from a particle IP, endpoint, or other identifier.<\/p>\n<pre>grep -c \"1.2.3.4\" \/var\/log\/nginx\/access.log<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-16132\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/f52533fd.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"645\" height=\"251\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>However, <code>grep<\/code>\u00a0is able to match multiple times per line. If you use the <code>-o<\/code>\u00a0flag, <code>grep<\/code>\u00a0will print out a new line per match. This doesn\u2019t work with the <code>-c<\/code>\u00a0flag, as it will only count matching\u00a0<em>lines<\/em>, not multiple matches per line.<\/p>\n<p>A better solution is to use the <strong><code>wc<\/code>\u00a0(word count) utility with the <code>-l<\/code>\u00a0(lines) parameter,<\/strong> which will count the raw number of lines passed to it over standard input. Using <code>wc -l<\/code>\u00a0is the preferred solution because it works with <code>-o<\/code>\u00a0to count the number of occurrences of the given string or pattern across the entire file.<\/p>\n<pre>grep -o \"foo\" file | wc -l<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-16134\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/d5027bbc.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"697\" height=\"373\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Counting_Across_Multiple_Files\"><\/span>Counting Across Multiple Files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A nice feature of <code>grep<\/code>\u00a0is the ability to handle multiple files at once, either passed through <code>xargs<\/code>, parameters, or supplied with wildcard expansion. When handling multiple files, <code>grep<\/code>\u00a0will print out the filename before the match. When using <code>-c<\/code>\u00a0to count the number of matching lines, it will also print out the filenames:<\/p>\n<pre>grep \"foo\" .\/*.txt -cH<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-16133\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/e26dd6d1.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"547\" height=\"305\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You should always use the <code>-H<\/code>\u00a0flag when working with the possibility of multiple files, since it will always print the filename even if there\u2019s only one file passed to <code>grep<\/code>. This will prevent automation from breaking if you depend on the filename being there.<\/p>\n<p>If you want to use <code>-o<\/code>\u00a0to count multiple matches per line, and pass the output to <code>wc -l<\/code>, you will unfortunately the ability to see the numbers for each individual file like with <code>-c<\/code>.\u00a0However, with a bit of <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>ing, you can chop the first column off with <code>cut<\/code>, and count the number of unique occurrences for each filename with <code>uniq -c<\/code>:<\/p>\n<pre>grep \"foo\" .\/*.txt -o | cut -d ':' -f 1 | uniq -c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-16135\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2022\/03\/c5ec5a47.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"781\" height=\"366\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><strong>RELATED:<\/strong> <strong><em>How to Extract and Sort Columns Out of Log Files on Linux<\/em><\/strong><\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/devops\/how-to-count-all-matches-of-a-string-with-grep-for-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;How To Count All Matches of a String With grep For Linux&#8221; grep\u00a0is a text search utility that can work with standard input or multiple files at once. It\u2019s used to print out matches for patterns, strings, or regular expressions. It\u2019s often useful to be able to count the number of matches, which grep can&#8230;<\/p>\n","protected":false},"author":1,"featured_media":475860,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/csit\/2017\/07\/add8ac45.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-475859","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/475859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=475859"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/475859\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/475860"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=475859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=475859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=475859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}