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 :
We want to search for the word ‘ERP’.
We’ll use the following syntax :
egrep 'ERP' article1.txt (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 :
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 :
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 :
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!