Computing Magazine

A Great Tool Called ‘Regular Expressions’

Posted on the 14 November 2011 by Skiabox @skiabox

reg_exp

The simplest way to use regular expressions is when we search for a single word.

For this article , we’ll use the command line tool called ‘egrep’ (egrep is available for many systems, like DOS, Windows, MacOS and Unix)

So let’s say that we have a file called article1.txt like this :

screen1

screen1

We want to search for the word ‘ERP’.
We’ll use the following syntax : egrep 'ERP' article1.txt (screen2)

screen2

screen2

As you can see , egrep returns all the lines that contain the word we are looking for.

That was a simple first example.

Now let’s say that we want to search another file that contains one word at every line for the word ‘cat’.
First we type more wordlist.txt at the command prompt to see what are the contents of this file :

screen3

screen3

Now if we use the same syntax, that we used in our first example, to search for the word cat egrep 'cat' wordlist.txt, the system returns two lines :

screen4

screen4

So how do we get only the lines that contain the word cat?
The answer here is to use metacharacters : ^(caret) and $(dollar)
The purpose of ^ and $ is to match the beginning and the end of the line respectively.
So let’s try this syntax now egrep '^cat$' wordlist.txtto see how it works :

screen5

screen5

That’s it!Now we can get only the lines that contain the exact word(s) we are looking for!
Stay tuned for more exciting regular expressions!


Back to Featured Articles on Logo Paperblog

Magazines