Grep Command in unix :
In previous article i have completed the tutorials on File commands,Directory commands and process commands of unix. In this article i will try to explain Grep Command which is used to search file or directory or string in a File or directory in unix operating system.Grep command actually searches the file which has the given pattern.Grep command is also used to search the string or regular expression in given file or files.
Syntax :
$grep options [Pattern] File1….File n.
Following are some useful options and real examples of Grep command :
To work with the grep command first we will first create a file using following command.And we will try to apply the options of the grep command.
Create a File :
$ cat>Grep_File
My Name is Amit
Having 5 Years of Experience in OBIEE and Oracle
The Website is named as complexsql Technologies
This institute is at Hadapsar Pune 411001
Planning for coaching classes with Complexsql
Complexsqltechnologies
Complexsqltechnologies
Complexsql
3591
5666
380792
46
Smidsy
. HiddenFiles
^ Start of the Word
* Any Number of Characters
&And much more
&And much more
And much more&
Ctrl+d
File named ‘Grep File’ is created.
1.Searching a given string in a File :
You can directly search the specified string in a file using grep command.But make sure that the search is case sensitive and the string which you want to search is also case sensitive.
Syntax :
$grep Search String Filename
Examples :
a) $ grep “complexsql” Grep_File
It prints the line containing complexsql.
Output :
The Website is named as complexsql Technologies
b) $ grep “Complexsql” Grep_File
It prints the line containing Complexsql
Output :
Planning for coaching classes with Complexsql
c) $ grep “Complex” Grep_File
It prints the line containing Complex
d) $ grep “Complexsql” file1 file2 file3
It searches ‘Complexsql’ string in file1, file2 & file3 files and print those lines.
e) $grep Complex*
It searches the Complex string in current directory all files.
Note: If string contains more than one word then it should be enclosed in double quotes
2.Case insensitive search using Grep / Option -i :
The Grep also used to search the string which is used for case insensitive search. We need to use the option named -i for case insensitive search.
Syntax :
$grep -i Search String Filename
Example :
$grep -i “complexsql” Filename
The above statement is used to fetch the lines which has complex word whether it is capital letters or in small letters.
Output :
The Website is named as complexsql Technologies
Planning for coaching classes with Complexsql
Complexsql
3.Counting the number of lines having that string / -c Option :
Using Grep Command user can count the number of lines which is having that specific string.
Syntax :
$grep -c Search String Filename
Example :
$grep -c “complexsql” Filename
Output :
3
4.Print the Lines with line number in a File / -n Option :
grep command is used to print the line with line number using -n option of Grep command.
Syntax :
$grep -n Search String Filename
Example :
$grep -n “complexsql” Filename
Output :
The Website is named as complexsql Technologies 2
4.Print the lines which does not contain the string / -v Option :
grep command is used to display the lines which does not contain the string.
Syntax :
$grep -v Search String Filename
Example :
$grep -v “complexsql” Filename
Output :
My Name is Amit
Having 5 Years of Experience in OBIEE and Oracle
The Website is named as complexsql Technologies
This institute is at Hadapsar Pune 411001
Planning for coaching classes with Complexsql
Complexsqltechnologies
Complexsqltechnologies
Complexsql
3591
5666
380792
46
Smidsy
. HiddenFiles
^ Start of the Word
* Any Number of Characters
&And much more
&And much more
And much more&
5.To search the filenames which contains that specific string / -l Option :
Grep command is also used to search the filename which contains the specific keyword.The option -l is used to search the filenames which contains that specific string.
Syntax :
$grep -v Search String Filename
Example :
$grep -v “complexsql” Filename
Output :
grep_file
6.To search the pattern which contains specific string / -o :
The grep Commands -o option is used to search the pattern which using that specific string.
Syntax :
$grep -o Search String Filename
Example :
$grep -o “complexsql” Filename
7. To highlight the pattern with colour / — color option :
To highight the specific pattern with the colour the user needs to use the — color option with grep command .
Syntax :
$grep –color Search String Filename
Example :
$grep -color “complexsql” Filename
The above command is used to highlight the lines which has word named ‘complexsql’.
Regular Expression:
Any string containing wild character is known as Regular Expression or Pattern.
These patterns are classified in 3 types:
1) Character pattern (Default)
2) Word pattern
3) Line pattern
1) Character pattern Examples :
a) $ grep “Complexsql*” Grep_File:
It prints complete line where the pattern is found
Output :
Planning for coaching classes with Complexsql
Complexsqltechnologies
Complexsqltechnologies
Complexsql
b) $ grep “I[tud]” Grep_File
It prints lines starting with I and any pattern matching in []
Output :
This institute is at Hadapsar Pune 411001
c) $ grep “N..e” Grep_File
It prints (.) any single character
Note:(.) is a wild card character, it matches any single character
2) Word Pattern:
a) \< \> OR –w : Word Boundary
b) \< : Start of the Word
c) \> : End of the Word
Real Life Examples :
1) grep “\<complexsql\>” Grep_File :
It Prints line containing the word ‘complexsql’.
2) grep -w “complexsql” Grep_File (-w option) :
It Prints line containing the word ‘complexsql’.
3) grep “\<complexsql” Grep_File
It Prints line starting with the word ‘complexsql’
4) grep “complexsql\>” Grep_File
It Prints line ending with the word ‘complexsql’
5) grep “\<[0-9]” Grep_File
It Prints line starting with number from [0-9] and any length.
Output :
3591
5666
380792
46
6) grep “\<[0-9][0-9]\>” Grep_File
It Prints line starting with any number from [0-9] and length upto 2
Output :
46
7) grep -n “\<[0-9][0-9]\>” Grep_File
It Prints number of lines and the matching pattern
8) grep -c “\<[0-9][0-9]\>” Grep_File
It Prints the count of matching pattern
3) Line Pattern:
a) ^ : Start of the Line
b) $ : End of the Line
Real Life Examples :
1) grep “^P” Grep_File
It Prints line staring with P
2) grep “s$” Grep_File
It Prints line ending with s
3) grep “^Th” Grep_File
It Prints line starting with the “Th” characters
4) grep “^[TH]” Grep_File
It Prints line starting with T or H characters
5)grep “^[^TH]” Grep_File
It Prints line which do not start with T or H characters
6) grep “\<The\>” Grep_File
It Prints line with Starting with “The” word
7) grep “^[0-9]” Grep_File
It Prints line staring with number from 0-9
8) grep “[0-9]$” Grep_File
It Prints line ending with number from 0-9
9) grep “^complexsql$” Grep_File
It Prints line having only word as “complexsql”
10) grep “\<Complexsql\>” Grep_File
It Prints line having word as “Complexsql”
11)grep “^….$” Grep_File
It Prints line having exactly 4 characters
12) grep “^\&” Grep_File
It Prints line Starting with “&” character
13) grep “$\&” Grep_File
It Prints line Ending with “&” character
14) grep -n “^$” Grep_File
It Prints Empty Lines
15) grep -v “^$” Grep_File
It Prints Non-Empty Lines
16) grep -c “^$” Grep_File
It Counts Empty Lines
Q) How to delete Empty Lines from a file?(important interview question)
$ grep -v “^$” Grep_File>Test
$ mv Test Grep_File
OR
$ grep -v “^$” Grep_File>Test | mv Test Grep_File
fgrep:
It is used to search multiple strings, but it doesn’t allow searching regular expressions. It searches the strings faster that the grep.
$ fgrep “Unix
Oracle
dba’’ Student
It prints line containing Unix, Oracle or dba from Student file
$ fgrep “Hari
Oracle
103” Student
It prints line containing Hari, Oracle or 103 from Student file
egrep:
It is combination of grep&fgrep plus some additional wild card characters.
Additional Wild Characters:
1) (|): It matches any one string in the given list.
$ egrep “Unix|Oracle|dba” Student
It prints line containing Unix, Oracle or dba from Student file
$ egrep “Hari|Oracle|103” Student
It prints line containing Hari, Oracle or 103 from Student file
2) (m): It matches exact occurrence of its preceding character
$ cat>Egrep_File
44555
45
100
5000
2000
300
12222
abbc
abbbc
abbbbc
abbbbbc
abbbbbbc
Ctrl+d
$ egrep“ab{2}c”Egrep_File
It matches exact occurrence of its preceding character
$ egrep “\<[0-9]{2}\>” Egrep_File
It matches exact 2 digit numbers
3) (m,n): It matches minimum ‘m’ occurrences and maximum ‘n’ occurrences of its preceding character
$ egrep “ab{3,5}c” Egrep_File
It matches minimum 3 & maximum 5 of ‘b’character
$ egrep “\<[0-9]{2,5}\>” Egrep_File
It matches 2 or 3 or 4 or 5 digit numbers
4) (m,): It matches minimum ‘m’ occurrences of its preceding character
$ egrep “ab{3,}c” Egrep_File
It matches minimum 3 & maximum ‘n’ of ‘b’character
$ egrep “\<[0-9]{3,}\>” Egrep_File
It matches minimum 3 digit numbers & maximum ‘n’ digits
bc command:
bc command use to open the Unix calculator and perform the arithmetic operations
$ bc
6+10
16
CTRL+D
Click on Topic You want to learn:
- History of SQL
- SQL Create Table(DDL in SQL)
- SQL DML Statements(INSERT,UPDATE,DELETE)
- SQL Select Statement Execution
- Operators in SQL
- Views in SQL
- Materialized View in SQL
- Joins in SQL
- Inner Join / Outer Join
- Full Outer Join / Cartesian Join
- Union and Union ALL
- Intersect and Minus
- Indexing in SQL
- Rank and Dense Rank
- SubQueries and Correlated Subqueries
- Parser and Optimizer
- Oracle 11 G new Features
- SQL Functions List
- Constraints in SQL
- Database Normalization
- Table Partitioning
- Pivot in SQL
- Difference Between Truncate,Delete and drop
- Oracle System Tables
Unix Tutorials :
4.Create File in Unix using multiple ways
12.paste Command
Hope with this article everyone get the idea about grep command and its examples.If you like this article dont forget to comment in comment section.