{"id":5155,"date":"2023-02-14T14:22:28","date_gmt":"2023-02-14T14:22:28","guid":{"rendered":"https:\/\/arzhost.com\/blogs\/?p=5155"},"modified":"2025-09-26T15:43:49","modified_gmt":"2025-09-26T10:43:49","slug":"grep-for-multiple-strings-patterns-or-words","status":"publish","type":"post","link":"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/","title":{"rendered":"How to Grep for Multiple Strings, Patterns, or Words?"},"content":{"rendered":"<p>Grep is a strong tool that comes with UNIX-based systems by default. How to Grep for Multiple Strings, Patterns or Words? Global Regular Expression Print is the name.<\/p>\n<p>You can control how the program looks for a pattern, or multiple patterns in this example, by using the grep command. Multiple strings can be found with grep in various files and locations. The search tool prints all lines that match the search pattern you enter.<\/p>\n<p>The usage of grep to search for multiple words or string patterns is demonstrated in this guide. To understand how to use grep most efficiently, pay attention to the examples in this article.<\/p>\n<ol>\n<li>UNIX-like or Linux-based system<\/li>\n<li>Access to a command line or terminal<\/li>\n<li>A user with authorization to access the required directories and files<\/li>\n<\/ol>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" 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: #999;color:#999\" 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><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#How_to_Grep_Multiple_Patterns_%E2%80%93_Syntax\" >How to Grep Multiple Patterns \u2013 Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#What_is_the_Difference_Between_grep_grep_-E_and_egrep\" >What is the Difference Between grep, grep -E, and egrep?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#Examples_of_Multiple_String_Pattern_and_Word_Grep_Searches\" >Examples of Multiple String, Pattern, and Word Grep Searches<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#How_to_Grep_Multiple_Patterns_in_a_File\" >How to Grep Multiple Patterns in a File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#Find_Several_Exact_Matches_in_a_File\" >Find Several Exact Matches in a File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#When_using_Grep_for_several_strings_ignoring_the_case\" >When using Grep for several strings, ignoring the case<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#Grep_for_Multiple_Patterns_in_a_Specific_File_Type\" >Grep for Multiple Patterns in a Specific File Type<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#Search_Recursively_for_Multiple_Patterns_in_a_File\" >Search Recursively for Multiple Patterns in a File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/arzhost.com\/blogs\/grep-for-multiple-strings-patterns-or-words\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Grep_Multiple_Patterns_%E2%80%93_Syntax\"><\/span><strong>How to Grep Multiple Patterns \u2013 Syntax<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When looking for numerous patterns in a file, the basic grep syntax entails using the grep command, strings, and the file&#8217;s name or path.<\/p>\n<p><iframe title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/yf7w-3aQeDg\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><u>The patterns must be separated by the pipe symbol and enclosed in single quotes. To use regular expressions, place a backslash before pipe |.<\/u><\/p>\n<ul>\n<li><code>fileName or filePath grep 'pattern1|pattern2'<\/code><\/li>\n<\/ul>\n<p><u>The -E option is the most recent way to use grep. With this selection, the pattern you selected is treated as an extended regular expression.<\/u><\/p>\n<ul>\n<li><code>grep -E \"fileName or filePath\" \"pattern1|pattern2\"<\/code><\/li>\n<\/ul>\n<p><u>Extended grep&#8217;s obsolete counterpart is egrep.<\/u><\/p>\n<ul>\n<li><code>fileName or filePath egrep 'pattern1|pattern2'<\/code><\/li>\n<\/ul>\n<p>Another choice is to use the grep command with numerous independent patterns.<\/p>\n<p><u><span style=\"color: #000000;\"><strong>How to Grep for Multiple Strings, Patterns, or Words?<\/strong><\/span> Use the -e flag and keep adding the required amount of search patterns to do this:<\/u><\/p>\n<ul>\n<li><code>pattern1 and pattern2 in grep fileName or filePath<\/code><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"What_is_the_Difference_Between_grep_grep_-E_and_egrep\"><\/span><strong>What is the Difference Between grep, grep -E, and egrep?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Extended grep has been superseded by the outmoded grep command. It performs the same task as grep -E.<\/p>\n<p>Extended grep contains meta characters that were added later compared to regular grep, which does not.<\/p>\n<p><a href=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2025\/09\/what-is-the-difference-between-grep-grep_E-and-egrep.jpg\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone  wp-image-14060\" title=\"what is the difference between grep, grep E and egrep\" src=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2025\/09\/what-is-the-difference-between-grep-grep_E-and-egrep-300x157.jpg\" alt=\"what is the difference between grep, grep E and egrep\" width=\"732\" height=\"383\" srcset=\"https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2025\/09\/what-is-the-difference-between-grep-grep_E-and-egrep-300x157.jpg 300w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2025\/09\/what-is-the-difference-between-grep-grep_E-and-egrep-1024x536.jpg 1024w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2025\/09\/what-is-the-difference-between-grep-grep_E-and-egrep-768x402.jpg 768w, https:\/\/arzhost.com\/blogs\/wp-content\/uploads\/2025\/09\/what-is-the-difference-between-grep-grep_E-and-egrep.jpg 1200w\" sizes=\"(max-width: 732px) 100vw, 732px\" \/><\/a><\/p>\n<p>The question mark, curly brackets, and parenthesis are these symbols. In extended grep, the pipe character | is also considered as a metacharacter.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Examples_of_Multiple_String_Pattern_and_Word_Grep_Searches\"><\/span><strong>Examples of Multiple String, Pattern, and Word Grep Searches<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We advise generating a file with some text on which we will test out a few different use cases in order to ensure that you comprehend how to use grep to search multiple strings.<\/p>\n<p>In our situation, we gave the file the name sample.txt and then added some content. We kept the file in the test user&#8217;s home directory, at \/home\/test\/sample.txt.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Grep_Multiple_Patterns_in_a_File\"><\/span><strong>How to Grep Multiple Patterns in a File<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Instead of extended grep, we&#8217;ll use grep in the examples that follow. The backslash must be used before the pipe character.<\/p>\n<p>You must use the escape character (backslash) to instruct the grep command to handle the pipe differently because grep does not allow the pipe symbol as the alternation operator.<\/p>\n<p><u>Use this command, for instance, to look for the words &#8220;extra&#8221; and &#8220;value&#8221; in the sample.txt file:<\/u><\/p>\n<ul>\n<li><code>txt grep \"extra value\"<\/code><\/li>\n<\/ul>\n<p>The string you wanted to grep is highlighted in the output.<\/p>\n<p><u>If the same file is located in a different directory, you must either go there or use the complete path to the file:<\/u><\/p>\n<ul>\n<li><code>\/home\/test\/Desktop\/sample.txt grep \"extra value\"<\/code><\/li>\n<\/ul>\n<p>How to Grep for Multiple Strings, Patterns or Words? Continue adding terms in this way to search for more than two.<\/p>\n<p><u>Add the desired string of letters followed by a backslash and a pipe to find three words, for instance:<\/u><\/p>\n<ul>\n<li><code>grep sample.txt 'additional value service'<\/code><\/li>\n<\/ul>\n<p><u>Here is how the grep command above appears when run with the grep -E, egrep, and grep -e options:<\/u><\/p>\n<ul>\n<li><code>grep -E extra, value, and service sample.txt<\/code><\/li>\n<li><code>egrep \"additional|value|service\" sample.txt<\/code><\/li>\n<li><code>txt grep -e extra -e value -e service<\/code><\/li>\n<\/ul>\n<p>In subsequent examples, we&#8217;ll use grep, but you are free to use whichever syntax you like.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Find_Several_Exact_Matches_in_a_File\"><\/span><strong>Find Several Exact Matches in a File<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><u>Use the -w argument with the grep program to locate exact matches for multiple<\/u> <u>patterns:<\/u><\/p>\n<ul>\n<li><code>txt grep -w \"provide count\"<\/code><\/li>\n<\/ul>\n<p><u>For instance, the result below illustrates the distinction between searching with and without the -w option:<\/u><\/p>\n<p>The outcomes differ, as you can see. The first command displays every line containing the strings you entered.<\/p>\n<p>The second command demonstrates how to find exact matches across many strings using grep. Only the lines with the exact wording are printed in the output.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"When_using_Grep_for_several_strings_ignoring_the_case\"><\/span><strong>When using Grep for several strings, ignoring the case<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use the -i switch to ignore letter case while searching for multiple patterns to prevent missing anything.<\/p>\n<p><u>For illustration, the following command will disregard the case:<\/u><\/p>\n<ul>\n<li><code>txt grep -i \"Arzhost|linux\"<\/code><\/li>\n<\/ul>\n<p>The results demonstrate how the two commands are different. When there are numerous matches, all of the matches are returned if the -I flag is used and the letter case is ignored.<\/p>\n<p><u>How to Grep for Multiple Strings, Patterns, or Words? You achieve more outcomes in this way. This command&#8217;s -w parameter allows you to even more precisely limit the results:<\/u><\/p>\n<ul>\n<li><code>The number of multiple matches in a file is displayed.<\/code><\/li>\n<\/ul>\n<p>Let&#8217;s imagine you are keeping an eye on a log file to check if there are more warnings or notifications. When several matches return, you don&#8217;t want to see specific results.<\/p>\n<p><u>For instance, type the following to display the number of multiple matches in the bootstrap.log file:<\/u><\/p>\n<ul>\n<li><code>'warning error'<a href=\"https:\/\/www.cyberciti.biz\/faq\/linux-log-files-location-and-how-do-i-view-logs-files\/\" target=\"_blank\" rel=\"noopener\"> \/var\/log\/bootstrap.log grep<\/a> -c<\/code><\/li>\n<\/ul>\n<p>The number of matches is displayed in the output. This allows you to rapidly ascertain whether there were more warnings and faults.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Grep_for_Multiple_Patterns_in_a_Specific_File_Type\"><\/span><strong>Grep for Multiple Patterns in a Specific File Type<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Grep can be used to search numerous strings within a specific kind of file. Use an asterisk and the file extension in place of a file name if you want to monitor log files in a single directory or search through all text files.<\/p>\n<p><u>For instance, to search through all.log files in the \/var\/log\/ directory for errors and warnings, type:<\/u><\/p>\n<ul>\n<li><code>\/var\/log\/*.log grep \"warning error\"<\/code><\/li>\n<\/ul>\n<p>We will only display the number of matches in order to better illustrate how this option functions.<\/p>\n<p>How to Grep for Multiple Strings, Patterns or Words? The output lists every file that greps browsed for the strings you specified.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Search_Recursively_for_Multiple_Patterns_in_a_File\"><\/span><strong>Search Recursively for Multiple Patterns in a File<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When you use the asterisk wildcard, the grep command only looks in the current directory.<\/p>\n<p><u>Add the -R operator to grep to include all subdirectories when looking for multiple patterns:<\/u><\/p>\n<ul>\n<li><code>grep -R \/var\/log\/*.log \"warning error\"<\/code><\/li>\n<\/ul>\n<p>All files that the grep command discovered in the \/var\/log\/ directory and its subdirectories will be listed in the output.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Have you discovered How to Grep for Multiple Strings, Patterns, or Words? \u00a0or string patterns with grep in this tutorial. You learned how to use extended grep from the guide as well.<\/p>\n<p>You can improve your grep search by practicing with the examples in this article. For additional information, see our grep regex guide.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Grep is a strong tool that comes with UNIX-based systems by default. How to Grep for Multiple Strings, Patterns or Words? Global Regular Expression Print is the name. You can control how the program looks for a pattern, or multiple patterns in this example, by using the grep command. Multiple strings can be found with [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5986,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"table_tags":[],"class_list":["post-5155","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-window"],"_links":{"self":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/5155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/comments?post=5155"}],"version-history":[{"count":5,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/5155\/revisions"}],"predecessor-version":[{"id":14065,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/posts\/5155\/revisions\/14065"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/media\/5986"}],"wp:attachment":[{"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/media?parent=5155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/categories?post=5155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/tags?post=5155"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/arzhost.com\/blogs\/wp-json\/wp\/v2\/table_tags?post=5155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}