The “grep” command:

The name grep stands for global regular expression printer. The grep command is used to search more than one file for particular bit pattern globally and displays the line containing them, The syntax is:

grep char_pattern file[s] 

After the execution of this command, each line of each file that contain the specific  char_pattern will be displayed on the display monitor. If you specify more than one file then each line is also immediately preceded name of the file. If the char pattern does not exist in the specified file then the grep command simply displays a new prompt.

The “grep” command using a single file:

Let us search a particular char_pattern in a single file. For instance, let us suppose, we are going to find every occurrence of word great in the file players. First the contents of the file are:
$ ls players

Cricket is the most loving game in India. There are so many good players
that it is difficult to select which of them are great.
Sachin Tendulkar is one of the great player of all times.
He is the true son of India.
Of course Sunil Gavasker was a good one. But not great.
It is our good luck we are still watching Sachin playing for India.
$

If we want to search word great then we do as:
$ grep great players

that it is difficult to select which of them are great.
Sachin Tendulkar is one of the great player of all times   
Of course Sunil Gavasker was a good one. But not great.
$
   
If we want to search for “son of India “, we write the grep command as:
$grep “son of India” players
He is true son of India.
$

Here if you don’t use double quote signs then the spaces between son, of and India would not produce correct result. Now watch carefully the execution of the following grep command :
$ grep son of India players
grep: cant open of
grep: cant open India
$

Means here, son would be expected as a character pattern and of, India and players are expected as a file to be searched. Since there is no file called of and India therefore the grep command displays the message:
grep: cant open of
grep:Cant open India.

And then it proceeds to search the file players for the pattern son. Thus it, is necessary to use single quotes or double quotes on the multi words pattern string is:
$grep ‘son of India’ players      or
$grep “son of India” players 
               
The grap command using multiple files: ~

The grep command is more helpful when we search for a character pattern in two or more files. Let we search for the word great in three files players 1, players 2 and players 3.
$grep great players1 players2 players3

player1: There are so many great players in the cricket world.
player2: Mazor Dhyan Chand was a great player.
player3: Laila is daughter of great boxer Mohmad Ali.
$

Here the pattern great will found in file named as player1, player2, player3.
Options of grep command: ~

(i)    -c     :     display only the number of matches.
(ii)    -i     :    ignore case while searching
(iii)    -l    :    displays only the name of files, as well as the text itself
(iv)    -n   :    displays the line number, as well as the text itself
(v)    -s    :    displays no output or just error messages are suppressed
(vi)    -v   :    displays those lines that do not match the searched pattern

For example if we want to know how many times the pattern great occurred in a file players, then we write
$ grep –c great players

If we want to see only those lines, which do not contain the specific  pattern, then we use the grep command as:
$ grep –v great players 

We can also use more than one options with a grep command as:
$ grep great –i –c players 1 players 2.

Metacharacter: ~

We can also use metacharacters, special characters such as ?, *, ., [..]
For instance, the following grep command
$grep great players?

Searches all those files that matches upto players? To any 8 characters filenames starting with players. Here the special character, ?, stands for any one characters.

The fgrep and egrep command: ~:


The fgrep and egrep command are advanced pattern matching command. The fgrep command doesn’t use any meta character for its searched pattern. The primary advantage of fgrep it that it can also serch two or more than two strings simultaneously.
The fgrep command can be used like this:
$fgrep ‘good
bad
great’ userfile
$

Here a single quote is used to mark three strings as one argument. Here we are going to search three different strings good, bad, and great. The egrep command is used to search this in a more compact form than fgrep command:
$egrep ‘good | bad | great’ userfile

The egrep uses an or ( | ) operator to achieve this. Therefore egrep command is more compact and more versatile than fgrep. Another achievement of egrep is that we can make groups of different patterns. If we use | as operator.
$egrep ‘sunil | rohan gavasker ‘ players

Here sunil is first pattern and everything to the right is considered as second pattern.
If we want to search both sunil gavasker and rohan gavasker use this:
$egrep ‘(sunil | rohan)‘ players

Example: ~

The program given below represents the functioning of egrep and grep commands:
read statements is the shell’s internal tool for taking input from the user that is Making scripts interactive. It is used with one or more variables and input supplied . the standerd input is read into these variables. Let us suppose a script named as employee1.sh.
The script employee1 uses read to take a search string and file name from the terminal.
$ cat employee1.sh
# script : employee1.sh-interactive version
# the pattern and file name will be supplied by the user
echo “\n enter the pattern to be searched :\c”
read  pname
echo “\n enter the file to be used :\c”
read filename
echo “\n searching for $ pname from file $ filename\n”
grep “ $ pname “  $ filename
echo “\n selected record shown above \n”
\c
put the cursor at the end of echo argument root to the next line



Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments