I was reading about Regular Expressions today. Many of the examples used a command-line utility called egrep. Naturally I wanted to try it. I found it difficult to download, install and get egrep working with Windows, however, as egrep was originally written for the Unix operating system. After persisting for a while, however, I was able to make it work, as follows:
- Use the download link in Section 1.3 of this article.
- Copy the .exe file to the folder in which you typically use the command line. For example, I usually run command line statements in the root of my C:/ directory, so I copied the .exe there.
- Run the .exe by clicking on it.
- Open the command prompt and verify that you in the directory where you ran the .exe
- Type egrep
- If egrep has installed successfully you will see a return message of:
- Usage: egrep [OPTION] … PATTERN [FILE]… Try ‘egrep –help’ for more information’
- egrep is now ready to be used
Using egrep
egrep can be used to search files using regular expressions. It will return the lines on which a match occurred. A simple example is a text file I created named sports.txt (saved in the C:/ directory) that contains the following:
football
soccer
baseball
tennis
track
From the command prompt I run:
egrep “ball” sports.txt
The returned result is:
football
baseball
I can also return just the lines that begin with the letter t as follows:
egrep “^t” sports.txt
The returned result is:
tennis
track