Untitled Document
grep Command

grep command is used to search a pattern , word or a line from a file . It searches for the PATTERN of text that you specify on the command line, and outputs the results for you. We have seen th detailed use of grep command in chpater - 12 . Lets see some more examples on grep command .

1. Basic Example -

Print the list of employess whose deigntation is "Consultant" from emp.csv

   grep "Consultant" emp.csv     

2. Searching in two files -

We can give multiple files to grep command .The below example searches the "Consultant" from two files emp.csv and emp1.csv . Look how the o/p is printed on terminal , each line is preceded by the filename .

   grep "Consultant" emp.csv emp1.csv   

3. Ignoring Case ( -i ) option

Sometimes we look for a name but not sure of the case , then in such a case use -i ( ignore ) option . This option ignores case for pattern matching .

   grep "sharma" emp.csv   

4. Deleting Lines ( -v) option

we can play inverse role too with grep command ( -v ) options selects all lines except those containing the pattern . Thus we can select all employee from emp.csv by removing the Manager from search pattern . So the command will show the list of all employees except managers .

   grep -v "manager" emp.csv   

5. Displaying Line Numbers ( -n ) option

The -n (number) option displays the line number containing the search pattern , along with the lines . Lets run the grep command with -n option and see the result

   grep -n "Consultant" emp.csv   
Untitled Document