Using Grep & Regular Expressions to Search for Text

Published on February 21, 2024

Using Grep & Regular Expressions to Search for Text

Thanks to regular expressions, it’s possible to match a single character, a word, or a specific pattern of characters when using the grep command. Grep is a Linux command that helps search for a given character in a file, and depending on your search goal, there are different ways of using grep and regular expressions to search for text, as seen in the examples covered in this post.

Example 1: Basic Grep Usage

Using grep to filter a file is easy. Add the search filter, such as a string or word, and specify the target file. Once executed, the command will display the results of all lines containing the search word or character, as in the output below.

Example 2: Show Omited Lines

Sometimes, you may have a case where you only want to display those lines that don’t match the search pattern. Using example 1, if we wanted to display the lines that don’t contain “meta, adding the -v flag does the trick.

All the lines in the output below are those omitted from the search pattern as they don’t contain the pattern in example 1.

Example 3: Working with Cases

Grep is case-sensitive. However, you can display all results without considering their cases by adding the -i flag. The first example below shows case-sensitive results; only one line matches the pattern. However, adding the -i option gives a different output as it captures the pattern without considering the case variations.

Example 4: Get Line Number

In the case of a large file, especially when debugging or checking logs, getting the line number for the search results is handy. With the -n flag, the grep results will show the line numbers for the matched pattern on the left. Here’s an example.

So far, we’ve given basic examples of working with the grep command. The other examples below incorporate regular expressions in the search. Take a look!

Example 5: Working with Anchors

Our first regular expression example involves working with anchors to filter patterns whose characters are in the defined location of the search pattern. We will use the ^ symbol in the search criteria to work with anchors. The first instance is where we only want to show results where the search word “New” appears at the start of the line. Thus, our anchor will be placed at the “^New” position.

The output below compares how not adding the anchor gives different results than when we add the anchor.

Similarly, we can add the anchor at the end to only show results where our search character/word appears as the last word in the line. For that, replace the ^ anchor with the $ anchor and place it at the end, like in the example below.

Example 6: Matching Any Character

Sometimes, you may want to match anything containing any characters before or after. For that, we use the period character (.) expression. For our first example, we match any word containing “le” with any characters before it.

For that, we will have our grep command, as shown below.

Still, if you want to match words that contain any characters after them, add the period characters after the pattern, as shown below.

Example 7: Matching Set of Characters

Using the ^ symbol with [], you can match anything that doesn’t contain the specified characters. For instance, a pattern like “New[^a-c]” specifies that grep should return only the results that start with “New” and don’t have a, b, or c as the immediate character. In the below output, one of the results we get is “New2c.” 2 is the immediate character after “New,” confirming that our regular expression works.

In the second example shown below, the “New[^1-5]” matches words that start with “New” but don’t have 1, 2, 3, 4, or 5 as the immediate character.

Example 8: Working with []

In regular expression, [] matches any characters inside it. The below example matches anything that starts with “New” and has a, b, or c as the immediate character.

Example 9: Working with a Range

You can create a range of characters you want to match in your search pattern. Instead of manually adding all the characters like in the previous example, you can create a range such as [a-d], which returns all matches starting with “New” and having a, b, c, or d as the immediate character.

Still, on the ranges, you can create two ranges to use for the same pattern. A range such as “New[a-d][0-5]” matches anything that starts with “New” and has a, b, c, or d as the immediate character, followed by 0, 1, 2, 3, 4, or 5 as the next immediate character. Check the example below.

Switching the arrangement to start with [0-5][a-d] gives different results as the order of the immediate characters must be met.

Here’s an example of a different range arrangement.

Example 10: Specifying the Regular Expression

You can create a custom regular expression to achieve the desired result. An expression such as “[nN]ew[0-5][a-f]” implies that we want to match anything that starts with n/N followed by ‘ew’ then [0-5] and [1-f].

Running the pattern gives the below results.

Conclusion

There are numerous instances where you must use the grep command to simplify your search. Combining grep with regular expressions gives you more leverage to search better and obtain more specific results, especially when working with large files. Besides, you can implement the grep with regular expressions when creating scripts. Hopefully, the ten examples presented in this post will guide you on how to work with grep and regular expression. Keep practicing to grasp the logic.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price