Get to know Linux: Using grep
If you use Linux long enough, you are going to wind up getting to know (and using) the command line. And if you use the command line long enough, you are going to find yourself using the grep tool. Grep is one of the most useful linux utilities in that it will search WITHIN a text file for a string of characters. Grep is such a useful tool that it is often used in shell scripts and much, much more.
In this article I am going to introduce you to the grep command and how it is used. Once you know this command, your life with the Linux command line will be made much easier.
Limitations
Although grep is a very versatile command to know, it does have its limitations. One of the biggest limitations it has is that it can not search within binary files. That's all fine, because your typical binary file would be searchable with the application that created said binary file.
Grep also has a line limit of 2048 lines. This means if your file is beyond that, grep will stop searching at the line limit.
Installation
You are in luck. Grep is a tool that comes pre-installed with all Linux distributions. So no installation is required.
Usage
The basic usage of grep is:
grep [OPTIONS] [PATTERN] file
Their are numerous options to use with the Grep command. The more helpful of these options are:
-E Interpret the PATTERN section as an extended regular expression.
-P Interpret the PATTERN section as a Perl regular expression.
-e Use PATTERN as the pattern. If searching a single PATTERN you do not need the -e option. But this option allows you to search for multiple search patterns.
-f  Obtain patterns from a file, one per line.
-i Ignore case.
-c Suppress normal output and only output count of matching lines.
--color Display the matched strings in color.
-n Display the line number associated with the matching entry.
Examples
Let's take a look at a very basic example first. You want to search the file /etc/test.conf for the string input. The grep command for this would be:
grep input /etc/test.conf
Now, let's say that test.conf is a larger file and you need to know the line number the string input is on. For this the command would look like:
grep -n input /etc/test.conf
The above output would then include the line number associated with each matching entry. Now, let's say you wanted to search the same file for both input and output and you want to know the line numbers associated with each. For this the command would look like:
grep -n -e input -e output /etc/test.conf
But what if you only want to know how many times the string input is found in the file /etc/test.conf. For this you could use the command like so:
grep -c input /etc/test.conf
Final thoughts
Grep is one of those commands that you will use time and again, in many, various ways. I always tell new users that, once you reach the point where you begin using the command line, grep is one of the first commands you should master.
Advertisement
This is not correct — “Grep also has a line limit of 2048 lines. This means if your file is beyond that, grep will stop searching at the line limit.”
Grep can handle more than 2048 lines. What it cannot do is, it cannot process more than 2048 characters in a line.