{"id":3276,"date":"2025-02-19T20:41:47","date_gmt":"2025-02-19T20:41:47","guid":{"rendered":"https:\/\/foghosting.com\/what-are-regular-expressions-in-grep-regex\/"},"modified":"2025-02-19T20:41:47","modified_gmt":"2025-02-19T20:41:47","slug":"what-are-regular-expressions-in-grep-regex","status":"publish","type":"post","link":"https:\/\/foghosting.com\/blog\/what-are-regular-expressions-in-grep-regex\/","title":{"rendered":"What are Regular Expressions in Grep (regex)?"},"content":{"rendered":"<div>\n<h2><span id=\"Introduction\">Introduction\u00a0<\/span><\/h2>\n<p>We are sure you must have gone through a file in the command line somewhere in your working process. Have you ever tried to find a particular word or phrase? If you have done it just with your reading capability, then hats off to you. A grep tool is a better way to search in a command-line file.\u00a0<\/p>\n<p>A grep tool is used to achieve perfect results in a short period. The use of <strong>regular expressions <\/strong>or regex in grep is pretty common as it enhances the power of this searching tool to manifolds.<\/p>\n<p>This whole article revolves around grep and regex. Hold on to your horses before you ask about <strong>regular expressions<\/strong> because we have separate headings to describe the term with examples. Read the complete article to get a fair idea about <strong>regular expressions in grep<\/strong>.<\/p>\n<h2><span id=\"Grep_Regular_Expression\">Grep Regular Expression\u00a0<\/span><\/h2>\n<p>A simple searching tool for command-line files is empowered with the combination of regex or <strong>regular expressions<\/strong>. Regex has a set of patterns that you can mix and match to achieve the perfect search result. The advanced feature was introduced due to the large command line file size, which sometimes takes time to search. <strong>Regular expressions in grep<\/strong> are known to provide solutions for repetitive and alternative searches. Do you know the regex grep is divided into three types at a broader level? The types are mentioned below.<\/p>\n<ul>\n<li>Basic Regular Expressions (BRE)<\/li>\n<li>Extended Regular Expressions (ERE)<\/li>\n<li>Perl-Compatible Regular Expressions (PCRE)<\/li>\n<\/ul>\n<p>PCRE is the most advanced regex with high-end metacharacters, while BRE is the most basic regex. Now let us move forward to <strong>grep regex examples.<\/strong><\/p>\n<h2><span id=\"Grep_Regex_Example\">Grep Regex Example\u00a0<\/span><\/h2>\n<p>We will compare a single example with two different approaches to better understand regex. The first approach will not use regex, while the second one will use regex.<\/p>\n<p>Example: There is a file named \u2018wonders.txt,\u2019 which has the below content.<\/p>\n<p>apple: 5<\/p>\n<p>banana: 10<\/p>\n<p>orange: 7<\/p>\n<p>grape: 3<\/p>\n<p>cherry: 6<\/p>\n<p>watermelon: 8<\/p>\n<p>Approach 1: We have to find the word \u2018apple in every line with simple grep command. To act, we will use the below command.\u00a0<\/p>\n<pre><code>grep \"apple\" wonders.txt<\/code><\/pre>\n<figure><\/figure>\n<p>The result will be as follows.<\/p>\n<p>apple: 5<\/p>\n<p>Approach 2: Now, we have to search for all those lines that have numbers through regular expressions. You can try out the below command.<\/p>\n<pre><code>grep -E \"d+\" wonders.txt<\/code><\/pre>\n<figure><\/figure>\n<p>The result will be as follows.<\/p>\n<p>apple: 5<\/p>\n<p>banana: 10<\/p>\n<p>orange: 7<\/p>\n<p>grape: 3<\/p>\n<p>cherry: 6<\/p>\n<p>watermelon: 8<\/p>\n<p>In the second approach, you can see that we have used metacharacters to perform a next-level search. The character \u2018-E\u2019 is used to enable regex mode. Further, the character \u2018d\u2019 is used to match digits from 0 to 9. The last character, \u2018+,\u2019 is used to search for repetitive words, phrases, numbers, or symbols.<\/p>\n<h2><span id=\"How_to_Use_Regex_With_Grep\">How to Use Regex With Grep?\u00a0<\/span><\/h2>\n<figure><\/figure>\n<p>Do you know which is the default mode for grep? Well, it is BRE or basic regular expression mode. This mode has limited metacharacters. If you want to have specific search results, then you will have to enable extended grep mode by entering \u2018-E.\u2019 Now we will have a detailed look at various cases where we can use regex.<\/p>\n<h3><span id=\"Literal_Matches\"><strong>Literal Matches\u00a0<\/strong><\/span><\/h3>\n<p>As the name suggests, literal matches are used to search for exact matching words or phrases. Also, it is case-sensitive, meaning the upper and lower case can easily affect the search result. Look at this example to understand literal matches.<\/p>\n<p>Today is a good day to eat kababs.\u00a0<\/p>\n<p>You have to search for good from the above sentence through regex with grep. Use the below command.\u00a0<\/p>\n<p>grep \u2018good\u2019 file.txt<\/p>\n<p>The output will display the line containing the word \u2018good.\u2019<\/p>\n<figure><\/figure>\n<h3><span id=\"Match_Any_Character\"><strong>Match Any Character\u00a0<\/strong><\/span><\/h3>\n<p>This is a very interesting case where you will place a dot \u2018.\u2019 in the command, and any character can replace the dot. \u201d to specify the result. Look at the below text and follow the example.<\/p>\n<p>Volleyball<\/p>\n<p>Football<\/p>\n<p>Handball<\/p>\n<p>You have to get the result related to \u2018o\u2019 as the first letter and \u2018b\u2019 as the third letter. The second letter of the search pattern can be anything. You can use the below command to create the pattern.<\/p>\n<pre><code>grep 'a.e' file.txt<\/code><\/pre>\n<figure><\/figure>\n<p>The result will show both volleyball and football but not handball.<\/p>\n<h3><span id=\"Bracket_Expressions\"><strong>Bracket Expressions\u00a0<\/strong><\/span><\/h3>\n<p>Bracket expressions are best to use when you want to search for a group of characters. Take the example of the below content that consists of various number formations.<\/p>\n<p>123<\/p>\n<p>256\u00a0<\/p>\n<p>868<\/p>\n<p>168<\/p>\n<p>118<\/p>\n<p>Now you want to find the pattern starting with \u20188\u2019 and \u20182\u2019. Use the below command to do so.<\/p>\n<pre><code>grep '^[28]' file.txt<\/code><\/pre>\n<figure><\/figure>\n<p>Here the output will be 256 and 868. Also, you can notice we have used \u2018^\u2019 this metacharacter to justify the matching at the start of the pattern.<\/p>\n<h3><span id=\"Character_Classes\"><strong>Character Classes\u00a0<\/strong><\/span><\/h3>\n<p>If you want to search for a variety of characters, then this option can be very useful. Keep in mind to always use character classes inside the bracket. Have a look at this example below.<\/p>\n<p>I ate 2 burgers and 1 pizza today.<\/p>\n<p>Now we want to find out the digits from the above sentence. We will use this command to act.<\/p>\n<pre><code>echo \"I ate 2 burgers and 1 pizza today.\" | grep -E '[0-9]'<\/code><\/pre>\n<p>The output will be 2 and 1.\u00a0<\/p>\n<h3><span id=\"Quantifiers\"><strong>Quantifiers\u00a0<\/strong><\/span><\/h3>\n<p>If your search has a repetition, then a quantifier is the best way to filter your search results. Here is a list of quantifiers that you can use for your search, {n},+,*,{n,m},? Etc.<\/p>\n<h3><span id=\"Alternation\"><strong>Alternation\u00a0<\/strong><\/span><\/h3>\n<p>Have you ever faced a situation where you need your search pattern has a variety of alternatives? Let us have a quick look at the alternation case example.\u00a0<\/p>\n<p>I have 2 cars and 3 bikes.<\/p>\n<p>We will apply alternation to the above sentence to search for digits and any word starting with the letter \u2018c\u2019.<\/p>\n<pre><code>echo \"I have 2 cars and 3 bikes.\" | grep -E 'd+|cw+'<\/code><\/pre>\n<figure><\/figure>\n<p>In the above command, we have used \u2018d\u2019 for digits and \u2018w+\u2019 to match letters or words after the letter \u2018c\u2019.<\/p>\n<p>The output will be 2, 3, and cars<\/p>\n<h3><span id=\"Grouping\"><strong>Grouping<\/strong><\/span><\/h3>\n<p>Grouping is used for complex data at hand and to apply search results to only specific parts of the data. You can mix and match your search results with the help of grouping. Go through the below example.<\/p>\n<p>I want to have apple juice and pineapple juice.<\/p>\n<p>Here you apply the grouping on fruits and juices using the below command.\u00a0<\/p>\n<pre><code>(apple|orange) juice<\/code><\/pre>\n<figure><\/figure>\n<p>The result will be apple juice and pineapple juice. Did you notice how easily the grouping included different words with combinations?<\/p>\n<h3><span id=\"Special_Backslash_Expressions\"><strong>Special Backslash Expressions\u00a0<\/strong><\/span><\/h3>\n<p>If you are a fan of compact commands with regular expressions in grep, then a special backlash can satisfy your search result. Some example of special backslash includes:<\/p>\n<p>\u00a0d- used for numerical characters from 0 to 9.<\/p>\n<p>\u00a0D- used to exclude non-digit characters.<\/p>\n<p>\u00a0w- used to match letters containing both uppercase and lowercase.<\/p>\n<p>\u00a0W- used to search any non-word character (no digits, letters, or words).<\/p>\n<p>\u00a0s- used to search for whitespace characters like blank, tab, new line, etc.\u00a0<\/p>\n<p>\u00a0S- it searches all the non-whitespace characters like digits, alphabets, etc.<\/p>\n<p>\u00a0\\- used to search for literal backslash.<\/p>\n<h3><span id=\"Escaping_Meta-Characters\"><strong>Escaping Meta-Characters\u00a0<\/strong><\/span><\/h3>\n<p>Sometimes it is a requirement to avoid the meta character; this is where we use escaping metacharacters. You just have to put a backslash behind the meta-character to search for it literally. Take a look at the below situation.\u00a0<\/p>\n<p>\u2018*\u2019 is commonly used for zero or more repetition, but with \u2018*,\u2019 you are instructed to search for \u2018*\u2019 in the text literally.\u00a0<\/p>\n<p>Also Read: Steps to Unzip\/Extract tar.gz Files in Linux Using Command Line<\/p>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>This content explains regular expressions (regex) in grep and includes examples to show how it can be used. Grep is a tool used to search for text in files, and it becomes more powerful when used with regular expressions. Regular expressions consist of patterns that allow for more precise searches. A simple grep command can search for exact word matches, like finding the word \u2018apple\u2019 in a file using the command: grep \u201capple\u201d filename. By learning and using regex with grep, you can conduct more efficient searches, which can make your command-line file exploration and analysis more effective.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction\u00a0 We are sure you must have gone through a file in the command line somewhere in your working process. Have you ever tried to find a particular word or phrase? If you have done it just with your reading capability, then hats off to you. A grep tool is a better way to search<\/p>\n","protected":false},"author":3,"featured_media":3076,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[17],"tags":[],"class_list":["post-3276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dedicated-servers"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts\/3276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/comments?post=3276"}],"version-history":[{"count":0,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/posts\/3276\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/media\/3076"}],"wp:attachment":[{"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/media?parent=3276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/categories?post=3276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/foghosting.com\/blog\/wp-json\/wp\/v2\/tags?post=3276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}