How to Use Regular Expressions (regexes) on Linux

How to Use Regular Expressions (regexes) on Linux

This guide is about How to Use Regular Expressions (regexes) on Linux. So read this free guide, How to Use Regular Expressions (regexes) on Linux step by step. If you have query related to same article you may contact us.

How to Use Regular Expressions (regexes) on Linux – Guide

Regular expressions in Linux are special characters that help with searching for data and matching complex patterns. Regular expressions are abbreviated as β€œregexp” or β€œregex”. They are used in many Linux programs like grep, bash, rename, sed, etc. grep is one of the most useful and powerful commands in Linux for text processing. Grep searches one or more input files for lines that match a regular expression and writes each matching line to standard output. A pattern consists of operators, constructing literal characters and metacharacters that have a special meaning. GNU grep supports three regular expression syntaxes: basic, extended, and Perl-compatible.

In its simplest form, when no regular expression type is specified, grep interprets search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E option (or –extended-regexp). In the GNU implementation of grep, there is no functional difference between basic and extended regular expression syntax. The only difference is that in simple regular expressions, the metacharacters ?, +, {, |, (, and ) are interpreted as literal characters. To preserve the special meaning of metacharacters when using simple regular expressions, characters must be escaped with a backslash (). We will explain the meaning of these and other metacharacters later.

How to Use Regular Expressions (regexes) in Linux

Using “.” (dot) to match strings.

Using “.” we can find a string if we don’t know the exact string, or just remember only the beginning and end of the string, we can use “.” As a missing character, and will fill in that missing character. Let’s see an example for better understanding:’ This file contains the name of the fruit, and we are going to use regular expressions in this file.

Road map:

  • #!/bin/sh
  • # Basic Regular Expression
  • #1. Using β€œ.” to match the strings.
  • # loading the text file
  • fruits_file=`cat fruit.txt | grep App.e`
  • # here the original word (response) will be Apple,
  • # but because we don’t know the Apple spelling,
  • # let’s put a period (.) in this place.
  • echo “1. Using ‘.’ to find the original word, while the given word is ‘App.e’”
  • #displaying output
  • echo “Output:”
  • echo “$fruits_file”
  • Using β€œ^” (caret) to match start of string

    Using β€œ^”, we can find all strings starting with the given character. Let’s look at an example for a better understanding. Here we are trying to find all fruit names that start with the letter B:

    Road map:

  • #!/bin/sh
  • # Basic Regular Expression
  • #2. Using β€œ^” (caret) to match start of string
  • # loading the text file
  • fruits_file=`cat fruit.txt | grep ^B`
  • echo “2. Using ‘^’ to find all the words that begin with the letter ‘B’”
  • #displaying output
  • echo “Output:”
  • echo “$fruits_file”
  • Using β€œ$” (dollar sign) to match the final from string

    Using β€œ$” we can find all strings that end with the given character. Let’s look at an example for a better understanding. Here we are trying to find all the fruit names that end with the letter e:

    road map:

  • #!/bin/sh
  • # Basic Regular Expression
  • #3. Using β€œ$” (dollar) to match the final from string
  • # loading the text file
  • fruits_file=`cat fruit.txt | grep e$`
  • echo “3. Using ‘$’ to find out all the words ending with the letter ‘e’”
  • #displaying output
  • echo “Output:”
  • echo “$fruits_file”
  • Using β€œ*” (an asterisk) to find any number of repetitions of a string

    Using β€œ*”, we can combine up to zero or more occurrences of the string character. Let’s look at an example for a better understanding. Here we are trying to find all fruit names that have one or more occurrences of ‘ap’ one after the other.

    Road map:

  • #!/bin/sh
  • # Basic Regular Expression
  • #4. Using β€œ*” to find any repeating number of a string
  • # loading the text file
  • fruits_file=`cat fruit.txt | grep ap*le`
  • echo “4. Using ‘*’ to find all fruit names that have one or more occurrences of ‘ap’ one after the other”
  • #displaying output
  • echo “Output:”
  • echo “$fruits_file”
  • Using β€œβ€ (a backslash) to match the special symbol

    Using “” with special symbols like whitespace (” “), newline(“n”), we can find strings from the file. Let’s look at an example for a better understanding. Here we are trying to find all the fruit names that have space in their full names.

    Road map:

  • #!/bin/sh
  • # Basic Regular Expression
  • #5. Using β€œβ€ to match the special symbol
  • # loading the text file
  • fruits_file=`cat fruit.txt | grep ” “`
  • echo “5. Using ” to find out the name of all fruits that have single space in their full name”
  • #displaying output
  • echo “Output:”
  • echo “$fruits_file”
  • Using “()” (braces) to match regexp group

    Using β€œ()”, we can find matching strings with the pattern in β€œ()”. Let’s look at an example for a better understanding. Here we are trying to find all the fruit names that have space in their full name.

    Road map:

  • #!/bin/sh
  • # Basic Regular Expression
  • #6. Using β€œ()” (braces) to match the regexp group.
  • # loading the text file
  • fruits_file=`cat fruit.txt | grep -E “(fruit)”`
  • echo “6. Using ‘()’ to find the name of all fruits that contain the word ‘fruit’”
  • #displaying output
  • echo “Output:”
  • echo “$fruits_file”
  • Using “?” (question mark) to find all matching characters

    Using β€œ?”, we can combine 0 or 1 repetition of the previous one. For example, if we do something like this: ab? It will match ‘a’ or ‘ab’. Let’s look at another example for better understanding. Here we are trying to find all fruit names that have the character ‘Ch’ in them.

    Road map:

  • #!/bin/sh
  • # Basic Regular Expression
  • #7. Using β€œ?” (question mark) to match the
  • # loading the text file
  • fruits_file=`cat fruit.txt | grep -E Ch?`
  • echo β€œ7. Using ‘?’ to find out all the fruit names that have ‘Ch’ in it”
  • #displaying output
  • echo “Output:”
  • echo “$fruits_file”
  • Final note

    I hope you like the guide How to Use Regular Expressions (regexes) on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.

    We will be happy to hear your thoughts

    Leave a reply

    How to Guides, Best Lists, Top 10 and Tech News - Compsmag
    Logo