The different tools integrated with Linux to help Linux administrators perform some tasks makes Linux interesting when it comes to ease of use in administering commands.

There are lots of ways in which one can locate files in Linux, but there is a different tool called grep. The grep command stands for Global Regular Expression Print. It is a special tool in Linux that’s used for enabling applications to sift through data looking for specific content and as well display a matched pattern to the screen.

Every Linux administrator needs this command to perform a search within the Linux environment. The grep command is a regular expression, and as such, they have their own syntaxes and rules that guide how they are used. In this article, we will be looking at some of the syntaxes used in creating regular  expressions, and we will also be looking at some few examples to get us acquainted with the command.

First, we will start by using grep command at its simplest term before advancing other ways in which grep command is applied in Linux.

The simplest way to use the grep command is to search for a file in which there is an exact match in the data that has been searched.

To get this clear, let's create a simple text file with a list of items as content. This will help us understand the simplest way in which the grep command operates.

Using the grep command to search for an exact match of a word in a file. Note that using this simple term of file search will match the data regardless of where the search pattern is located on the data: beginning, middle, or the end of the data.

Try to use the man command to explore other options that can be used alongside with the grep command.

One other important option is the –I option. Let's see how that works when it is appended with the grep command

With the –i option, the grep command will ignore case distinction in both patterns. In this case, the user wants data displayed from the search pattern provided, ignoring the case-sensitivity from the loop of contents in the file.

We can also use the –w option to search for data that match the whole word. By default, the grep command match string from a data even if it is found as a substring in a file. With the –w command, the grep will only display the
exact word as it is in the file.

The search pattern is “ear,” and you can see that only the word with that exact search pattern was displayed

By default, the grep command displays the entire word or line where the matched pattern is found. We can as well make the grep command to show only those matched patterns using the –o option as seen below

To look up files in an entire directory tree, you use the –r or –R option

They are lots of options available when using the grep command. You can look up to that and search for patterns as per need

HOW TO USE LINE ANCHOR TO SEARCH DATA

Unlike the conventional way of searching files using the grep command, you can also control how files are being searched using a line anchor. The line anchors are the (^) and the ($). The (^) anchor can be used at the beginning of a line while the ($) is used at the end of a line.

Let's take a look at how these line anchors work in searching for data. We will be using the same file created initially to perform this operation.

Adding the ^ anchor at the beginning of the search pattern will only display data that matches the word “tea” at the beginning of the data.

Let's take a look at the other anchor which is used at the end of the search pattern

In a situation where you only want to display the word that ends with “ear,” append the $ sign at the end of the search pattern.

You can also use both anchor signs together to perform your search operation. This happens in a situation when you want the exact match as the search pattern to be outputted on the screen. Let's take a look

HOW TO USE WILDCARDS AND MULTIPLIERS TO SEARCH FOR DATA

Wildcard refers to characters that can be substituted for zero or more characters in a string. In a regular expression, a period (.) matches a single character. Let's take a look at an example to show how it is applied as a regular expression.

From the command above. Using period (.) in between your search pattern tells the grep command to look for a data that starts with p, followed by anyone character and followed by another character r

Another way to use a wildcard in a regular expression is to create a set of acceptable characters as a search pattern. With this, the grep command could display all data in regards to the characters specified. Let's say you want to view all data in a file that starts with an “e” and have an “e” or an “a” in-between followed by another character called “r” We can do that as follows.

Having known how wildcards are used in regular expressions, we should look at how multipliers are applied in regular expressions. Multipliers are often used alongside with the wildcards. The most used multiplier is the *. When the * is used in a regular expression, it signifies many characters. For example, when the * sign is used as go*, it tells the grep command to print out all data where I have g or o and can include any other characters as many as possible. When used alongside the period sign (.). For example, when used as go.*, it tells the grep command to print out data that MUST start with “g and o” and any other character

Let's give it a practical example from the command line

HOW TO USE GREP TO SEARCH FILES WITH PIPED COMMAND

Other from the method that has been discussed above on how grep command is used in a regular expression, we can also be used in conjunction with other commands using a |. This helps to filter out information from the supposed output of a command.

Let's try to view our log files and use the pipe command in conjunction with our regular command to view log files

Regular expressions may sometimes contain metacharacters like the $ or * sign. It is important to encapsulate the regular expression with a quote to give a different meaning to what you intend to search for.

CONCLUSION

Like I always say, try to explore your knowledge in learning how various commands work. They are lots of different options available and as well as different ways to search for data in Linux. You might end up not using a lot of options available on the man page, but for knowledge sake, it is important you get yourself acquainted with these options.

If you are finding any of the options difficult to use, feel free to drop a comment on the comment section. If you also want to add to this article, you can as well leave a comment, and we will attend to that.

Author

Comments