Grep Fundamentals: Recognising Search Patterns Using Grep & Regular Expressions

Grep Regular Expression

Have you ever wanted to search for a particular text in a document but found it significantly harder to search for text patterns manually? You will never face this problem again since we have a solution made just for you!

Grep (Global regular expression print) is a utility that uses "regular expressions" to describe a search pattern. These search patterns can be recognized in text files and code written in different programming languages.

Those of you who are comparatively new to the world of grep commands might wonder how searching for a specific word in a document could be that hard.

To clear your confusion, it is important to mention that even though the basic regular expressions may be geared towards text files, more advanced regular expressions work in complex situations and coding programs.

What is a Grep Regular Expression?

A grep regular expression is a collection of multiple characters that define a specific pattern. This pattern of literal characters is compared to all other text in a file. If the text or code in the file matches the specified pattern stated by the user, the text is highlighted to the user.

You might be confused by the influx of technical terms used above. To break it down into simple words, This article will describe in detail all the components of a grep regular expression.

Regular expressions

Different operating systems and command line interfaces have their own different variations of regular expressions. However, regular expressions in Grep are generally composed of literal characters and meta characters.

Literal Characters

Literal characters are special characters that only represent a single character, i.e., 'a' and '8' are literal characters. These characters match the pattern literally. This literal match provides more accuracy and precision to the users. Literal characters may include the following characters:

  • Regular character

  • Period character

  • A character string

  • Numerical characters

  • Brace characters

Meta Characters

A Meta character on the other hand is a character that has a special meaning which is known by the computer program beforehand. The meta characters have a pre-defined meaning in the computer interface which helps in making patterns. These meta-character patterns further aid in text searching.

Some regular meta characters are '()' and '^'. Other meta characters also include '*' and '.' with different pre-defined meanings in operating systems.

Meta Characters

Grep

As mentioned above, Grep (Global regular expression print) is a utility that uses regular expressions to describe a search pattern.

The regular expressions in Grep include more than just a literal character or a meta-character. These regular expressions also include anchors, escape characters and character classes.

Anchors

Anchors in a grep command specify the position where a match should occur in a text. The two main anchors with a specified location are matches at the start and at the end of a line. These anchors help to identify complex matches.

Escape Characters

An escape character refers to a backslash '\' in different programming languages. This escape character is referred to as a literal character. On the other hand, the escaped pipe character is a metacharacter that performs alteration. This character is written as '|'.

Character Classes

These character classes usually refer to square brackets consisting of two characters '[ ]'. Users can insert any words to search string in a text file. This bracket expression matches the specified criteria in the files. For example, if a user inputs the following command, '[aeiou]', the command will match the input to any vowel.

What is a grep command?

A grep command is a sequence of characters in an expression that allows the user to search for a specific pattern in input files or text streams. The grep command makes use of the BRE syntax. This basic syntax, which is used for the grep regex patterns, is given as:

grep [options] pattern [file...]

What is a grep command

The syntax for these grep searches includes some important keywords in the bracket expressions. The 'options' written in the first bracket expression represent the modifiers to filter searches. The 'file' in the second of the bracket expressions specifies the file to be searched.

An example of a grep regex command is given as:

grep -n "error" logfile.txt

The above example will display any lines that contain errors in the log file and the associated line numbers to make identifying code easier for the user. This line of code is essential to find lines of code where desired instances occur.

Types of Regular Expressions

A regular expression has several different forms with several different functionalities. However, you must make sure to choose a type of regular expression depending on the complexity involved in your desired pattern matching.

The BRE format is typical for simple text patterns; however, it may not work smoothly with complex operations. Let us explore the different types of regular expressions:

Basic Regular Expressions

A basic regular expression is the most widely used format in grep regex patterns. The expression mechanisms used in the BRE version are very simple and beginner-friendly.

In these types of expressions, most meta-characters lose their original meaning unless a backslash is put after the following pattern, i.e., '$\'. If the first character of these expressions is written by itself, then the meta-characters lose their special meaning altogether.

The following command is used to search for a specific string in the configuration files:

grep "pattern" filename

The expression for searching for a word at the beginning of a line is given in the following example:

grep "^pattern" filename

Similar to the previous example, the expression given below is used to search for lines starting with a specific pattern followed by any characters:

grep "^pattern.*" filename

Another common expression to search for lines containing any one of multiple words is given in the following expression:

grep "word1\|word2" filename

Types of Regular Expressions

Extended Regular Expressions

Extended regular expressions tend to expand further on BRE and introduce meta characters without making the use of escape expressions necessary. They also provide other expression mechanisms besides the basic usage of BRE. To enable the ERE option in 'grep regex', users can use the '-E' option.

Another version of the search for lines containing either "word1\|word2" in ERE form for alternative matches is given as:

grep -E "word1\|word2" filename

The following command shows how to search for lines with zero or more times a character occurs in a text file:

grep -E "char" filename

To search for lines with any single character, the following expression is given as user input:

grep -E "a.b" filename

The following example is given as input to search for lines with a pattern that has a preceding character pattern in the text:

grep -E "pattern1.*pattern2" filename

To search for lines with a specific character range of lowercase letters 'a-z' in a file, the following pattern is written:

grep -E "[a-z]" filename

The following output of the given command can also be replicated for uppercase letters. For the uppercase letters, the two characters in the bracket will be changed from 'a-z' to 'A-Z'.

grep -E "[A-Z]" filename

Pеrl-Compatiblе Rеgular Exprеssions

This type of rеgular еxprеssion is a highly advanced format, so it is not as commonly used for grеp rеgеx. Thе Pеrl-compatiblе rеgular еxprеssions arе madе to bе usеd with thе spеcial charactеrs of thе Pеrl syntax.

Thеsе еxprеssions in grеp rеgеx includе thе lookahеad and lookbеhind assеrtions. To еnablе thе PCRE option in 'grеp rеgеx', usеrs can install thе 'pcrеgrеp' option at thе beginning of a linе.

To sеarch for linеs starting with any charactеrs that havе a prеcеding charactеr as "pattеrn", thе command is:

pcrеgrеp "word1|word2" filеnamе

Writing thе pcrеgrеp at thе vеry bеginning of a grеp rеgеx еxprеssion is thе only thing that nееds to bе donе to usе its fеaturеs. Thе command to sеarch for linеs with thе occurrеncе of a charactеr for zеro or morе timеs is givеn as:

pcrеgrеp "colou?r" filеnamе

To find thе еxact match for all thе linеs with a pattеrn followed by another pattеrn, thе sourcе codе is givеn as:

pcrеgrеp "pattеrn1.*pattеrn2" filеnamе

Anothеr command to find thе еxact numbеr of linеs with a specific numbеr of charactеrs in a word is:

pcrеgrеp "\<word_lеngth\>" filеnamе

Using a basic regular expression in a file

To use a BRE in a command line file, your syntax will include the word grep which is the command, your pattern, a term used to express what you are looking for, and lastly the filename which shows where you are searching.

grep "pattern" filename

Using a basic regular expression in a file

Searching for a specific string

To search for a specific string in the file, given it is not an empty string and the file does not have any empty lines, the command below will display all the lines in the file that contain the string given by the user:

grep "you need to install pcregrep as it is" file1.txt

However, it should be noted that in most cases, the example to match will locate specific words within the quotation. The only variant “you need to install pcregrep as it” will be used, and no others such as, “you need to install pcregrep as it” or “YOU NEED TO INSTALL PCREGREP AS IT”.

This correlates to a case-sensitive search that you can use in your system to find any variations of the above example.

grep -i "you need to install pcregrep as it is" file1.txt

This statement will match lines with "you need to install pcregrep as it is", "You need to install pcregrep as it is" and "YOU NEED TO INSTALL PCREGREP AS IT IS".

Displaying line numbers

Through the advanced features of our Linux system, you can also search for the line number along with the matched lines. With the help of the line numbers, users can locate the specific occurrence of a pattern in a file.

grep -n "you need to install pcregrep as it is" file1.txt

In addition, users can find the line numbers for patterns that are not included in the specified pattern.

grep -v "you need to install pcregrep as it is" file1.txt

If the example to match has no other lines in the files then the empty lines will be shown as the output.

How to check regex in Linux?

There are multiple ways you can search for special patterns or even regular patterns in Linux. One way includes using the built-in echo function of Linux. This word is written at the start of the of the expression in the command:

echo "you need to install pcregrep as it is" | grep -E "install pcregrep"

Do not panic if after running the command ‘do not echo’ appears useless because you can proceed with it as normal, without necessarily providing an “echo” statement in the source code. Using 'grep' directly with the files is written as:

grep -E "install pcregrep" file1.txt

Another commonly used command for implementing the regex function in Linux is using 'regex' itself. The above expressions will be written in the form:

echo "you need to install pcregrep as it is" | regex "install pcregrep"

The echo command may or may not be used when using the regex function, but it is sometimes essential to use echo to interact with the command line.

What is an invalid regular expression?

Learning the basic syntax and sequence of characters for writing grep commands is very important for beginners. An invalid regular expression could be a pattern that does not form the particular syntax rules of the language used. An invalid regular expression usually is a pattern that leads to syntax, runtime, or other issues.

Syntax Errors

Regular expressions usually have a specific syntax depending on the programming language being used. It is imperative for users to follow this syntax when writing code and giving instructions to the system.

Some examples of common syntax errors programmers make while writing code are given:

  • An unclosed bracket

  • An invalid quantifier

  • Misspelling of keywords

Unsupported Features

There are certain features and keywords that are not present in every regular expression engine. Users are unable to use such features since there is no support. Once the system detects such users’ activity, it fails to provide the right results by giving out an error message.

  • (?<=lookbehind) or (?<=status>...) are not features supported in all regex functions.

Unescaped Special Characters

There are certain characters with a special or pre-defined meaning in regular expressions. If a user wants to match them literally, they first need to be escaped. Without escaping these special characters, the expression may become invalid.

  • Using an * without preceding character or escape

Conclusion

It is essential for all beginners to be aware that they should have a good understanding of the fundamental command structure of this language prior to using it. convert written to human Basically, the main lesson to be learned from reading this article is that it’s not necessary to plunge into complex algorithms and patterns. Take your time learning the basics, and move on gradually.

Mastering the skills of using grep and regular expressions is not easy, but very fruitful. We've provided our users with the best instructions and knowledge to help them navigate the world of grep and regular expressions.

If you can correctly learn the use of effective expressions and how to arrange those expressions to find the appropriate patterns you need to find in a file, then that means you know everything you need to know about grep!

Ozair Malik
The author
Ozair Malik

Ozair is a Cyber Security professional hailing from Islamabad, Pakistan. He holds a bachelor's degree in Cyber Security and has been president of AUCSS (Air University Cyber Security Society) for 2021-22. Ozair's expertise lies in Technical content writing, Information Security, and Cyber Security. As the founder of CySecOps, a Cybersecurity company that specializes in SaaS & E-commerce digital security, Ozair is passionate about ensuring the safety of businesses online. Alongside his exceptional team building and leadership skills, Ozair is an analytical, observant & critical thinker. Stay up-to-date with Ozair's work by following him on Twitter @OzairMalik13, LinkedIn, or visiting his website at Cysecops.co.