{"id":18952,"date":"2022-10-04T16:10:31","date_gmt":"2022-10-04T16:10:31","guid":{"rendered":"https:\/\/linuxways.net\/?p=18952"},"modified":"2022-11-04T02:43:38","modified_gmt":"2022-11-04T02:43:38","slug":"grep-count-number-occurrences-word-linux-mint-21","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/mint\/grep-count-number-occurrences-word-linux-mint-21\/","title":{"rendered":"Use Grep to Count Total Number of Occurrences of Word in Text File in Linux Mint 21"},"content":{"rendered":"<p>\u201cGrep is a very useful Linux command that can perform a variety of different operations. One of the purposes for which this command can be used is to count the total occurrences of a specific word within a file. This guide will walk you through the complete procedure of counting the total number of occurrences of a word within a text file in Linux Mint 21.\u201d<\/p>\n<h2><strong>Using the Grep Command to Count the Occurrences of a Word in a Text File in Linux Mint 21<\/strong><\/h2>\n<p>If you want to find the total number of occurrences of any given word in a text file in Linux Mint 21 with the help of the grep command, then you should perform the following steps:<\/p>\n<h3><strong>Step # 1: View the Target File<\/strong><\/h3>\n<p>First, we will show you the target file in which we will be counting the occurrences of a particular word. To display the contents of that file, we will execute the command shown below:<\/p>\n<pre>\r\n$ cat grep.txt\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"327\" height=\"23\" class=\"wp-image-18956\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-1.png 327w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-1-300x21.png 300w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><\/strong><\/p>\n<p>The target text file is shown in the following image. From this file, we will be calculating the total occurrences of the word \u201cgrep\u201d by using the grep command in Linux Mint 21.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"379\" height=\"109\" class=\"wp-image-18957\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-2.png 379w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-2-300x86.png 300w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/p>\n<h2><strong>Step # 2: Use Grep to Count the Total Number of Occurrences of a Particular Word of the Target File<\/strong><\/h2>\n<p>Now, we will use the command shown below to count the number of occurrences of the specified word:<\/p>\n<pre>\r\n$ grep \u2013o \u2013i grep grep.txt | wc \u2013l\r\n<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"491\" height=\"25\" class=\"wp-image-18959\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-3.png 491w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-3-300x15.png 300w\" sizes=\"auto, (max-width: 491px) 100vw, 491px\" \/><\/strong><\/p>\n<p>Here, the \u201c-o\u201d flag is used to print only the matching words, the \u201c-i\u201d flag is used to ignore the case of the following word, \u201cgrep\u201d represents the word whose occurrences are to be calculated, \u201cgrep.txt\u201d is the name of the file in which we need to perform this operation whereas \u201cwc \u2013l\u201d is used to count the number of lines in which the match is found.<\/p>\n<p>The total number of occurrences of our specified word from the given text file is shown in the following image:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"507\" height=\"61\" class=\"wp-image-18961\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-4.png 507w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-4-300x36.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/10\/word-image-18952-4-501x61.png 501w\" sizes=\"auto, (max-width: 507px) 100vw, 507px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>This article was based on a very simple method for counting the total occurrences of a word within a text file in Linux Mint 21. By following this procedure, you will be able to find out the number of occurrences of any given word of your choice in a text file very easily.<\/p>","protected":false},"excerpt":{"rendered":"<p>Grep is a useful Linux command that can perform different operations. This command can be used to count the total occurrences of a specific word within a file.<\/p>","protected":false},"author":3,"featured_media":18966,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-18952","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mint"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=18952"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/18952\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/18966"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=18952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=18952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=18952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}