Learning Linux: Regex and grep

In a previous blog, I covered some basic grep commands that you can use to search log files. This post will delve a little further into searching files using regular expressions, or regex. Specifically, we’ll look at a couple of basic regular expressions called anchors. Anchors will help refine your searches and polish your ability to search for items more efficiently.

We will use an example file named text.txt which holds these contents:

Hello World! Hello World! (again)

This is the second sentence.
Here is another sentence.
That was the fourth sentence.
Here is the fifth.


In that file, if you want to search for the beginning of a line, you can use the ^ character. If you need to search for all lines that begin with the letter “H,” you can enter grep -n ‘^H’ text.txt. This will output:

1:Hello World! 2:Hello World! (again) 5:Here is another sentence. 7:Here is the fifth.

Note, the single quotes aren’t always needed depending on the grep pattern, but for the purposes of this post, they will be included. In addition, you can leave off the -n option; this just outputs the line number in the text file that the result can be found. Note that here, empty lines are counted. You can search by any number of beginning characters, such as "He, Her, Here," and the results will match accordingly.

The same can be done for lines that end in a certain character, or string of characters, by using the $ character. If you want to search for any lines that end in an "!", you might be tempted to do this: grep -n ‘$!’ text.txt. However, this is incorrect for two reasons. First, the "$" comes at the end of the search pattern, not before. Second, since "!" is a special character, you need to escape it with “\”. Therefore, your command should look like this: grep -n ‘!$’ text.txt. This outputs:

1:Hello World! 8:Goodbye!

You can also search for an exact match using both these anchors. For example, grep -n ‘^Hello World!$’ text.txt returns only:

1:Hello World!

Without the last "$" in the grep pattern, it would return two items:

1:Hello World! 2:Hello World! (again)

These are just a couple of basic additions you can add to your command line arsenal that will increase your ability to analyze files. Stay tuned next time for more Linux tips and tricks!

Protect Your Business Data

We are passionate about helping our customers protect their data. We want you to use Jungle Disk to protect yours. Click on Sign Up to get started. It takes less than 5 minutes!

Sign Up