0
3.3kviews
Explain use of grep command. Also give examples of use of c, v, and i option with grep.

Mumbai University > Information Technology > Sem 5 > Open Source Technology

Marks: 5M

Year: Dec 2015

1 Answer
1
13views

grep command: grep command is useful for searching the content of one more files based on the pattern. A pattern may be a single character, bunch of characters, single word or a sentence. When we execute the grep command with specified pattern, if its is matched, then it will display the line of file containing the pattern without modifying the contents of existing file.

The basic syntax of grep command is

grep [options] pattern [list of files]

Following example helps us in better understanding the use of grep command:

First create the following demo_file that will be used in the examples below to demonstrate grep command.

$ cat demo_file

    THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

    this line is the 1st lower case line in this file.

    This Line Has All Its First Character Of The Word With Upper Case.
    

    Two lines above this line is empty.

    And this is the last line.

1. Search for the given string in a single file

 The basic usage of grep command is to search for a specific string in the specified file as shown below.

 Syntax:

 grep "literal_string" filename

 $ grep "this" demo_file

this line is the 1st lower case line in this file.

Two lines above this line is empty.

And this is the last line.

Use of –i option with grep command:

Syntax:

grep -i "string" FILE

This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.

$ grep -i "the" demo_file

    THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.

    this line is the 1st lower case line in this file.

    This Line Has All Its First Character Of The Word With Upper Case.

    And this is the last line.

**Use of –v option with grep command:**

This option is used for Invert match. You can display the lines that are not matched with the specified search sting pattern using the -v option.

This example will display all the lines that did not match the word “go”.

    $ grep -v "go" demo_text

4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

Use of –c option with grep command:

When you want to count that how many lines matches the given pattern/string, then use the option -c.

Syntax:
grep -c "pattern" filename
$ grep -c "go" demo_text
    6

When you want to find out how many lines matches the pattern

    $ grep -c this demo_file
3

When you want to find out how many lines that does not match the pattern

$ grep -v -c this demo_file
4
Please log in to add an answer.