How to use grep command in Linux?

grep command in Linux is used to search for patterns within a file i.e., it will help to your search for content within a file. For example, if you want to search for “help” within a file f1.txt, then you can use grep command. A pattern is different from a word. For example, the as a pattern means all of these: the, then, there, 12the34. But the as a word means only the.

Syntax

grep [OPTIONS] PATTERN [FILE…]

grep prints the lines which contains the pattern

For Example

Create a file f1.txt which contains the following data:

This is the first line
It is in sentence case
Now Is written
In mixedcase and IS

                $grep “in” f1.txt

Output:

This is the first line
It is in sentence case

grep displays the lines containing the pattern in. The first line contains in as part of the word line and the second line has in as such.

grep by default is case sensitive. That’s why In in the fourth line of f1.txt was not the part of the output in the above example.

Options used with grep command in Linux

1. To print line numbers of lines containing the pattern

-n: option prints the line numbers along with the line. It might be the case that the file in which you are searching contains thousands of lines. In this case, just getting the line containing the pattern may not be sufficient because it will still require a considerable amount of effort to find the desired line in the file. Thus, printing the line number along with the line becomes beneficial. For example:   

using -n option with grep command in Linux

2. To count the number of lines containing the pattern

-c: option prints the count of lines containing the pattern rather than the lines themselves. For example:

using -c option with grep command in Linux

3. To ignore case-sensitivity

-i: options makes grep ignore case-sensitivity. Thus, it prints all lines containing the pattern in any case. For example:

using -i option with grep command in Linux

4. To print the lines not containing the pattern

-v: option prints those lines which do not contain the specified pattern. For example:

using -v option with grep command in Linux

5. To print names of all files containing the pattern

-l: option will print the names of files that contain the pattern. Suppose you want to know the names of all files containing a given pattern, then -l option can be used. For example:

using -l option with grep command in Linux

6. To search for multiple patterns

-e: option is used to search for multiple patterns. For example, if you want to search for either the or and then:

using -e option with grep command in Linux

Practice Questions on use of grep in Linux

For all these questions consider f1.txt (as defined above) as the input file

Q1. What will be the output of the following command

                $grep -vi “is” f1.txt

Q2. What will be the output of the following command

                $grep “Is” f1.txt

Q3. What will be the output of the following command

                $grep -cv “Is” f1.txt

Q2. How can you display all lines that contain either “line” or “ten”?

Useful Relevant Commands

ls
touch
chmod
useradd

Leave a Reply

Your email address will not be published. Required fields are marked *