grep
stands for Global Regular Expression Print
. This command is used to search for particular string or keyword from a file and print those lines matching a pattern in the shell. It checks line by line and print those lines matching a pattern. It can be use anywhere like with files, searching for file, directories, etc.
Grep command syntax:
grep [OPTION].. Pattern [File]..
Examples
Basic Example
1 2 3 4 5 6 7 8 9 10 11 12
#csv1 id,firstname,lastname,email,profession 100,Mady,Cloris,Mady.Cloris@gmail.com,police officer 101,Nita,Eiser,Nita.Eiser@gmail.com,worker 102,Ada,Rosette,Ada.Rosette@gmail.com,worker 103,Demetris,Teryn,Demetris.Teryn@gmail.com,worker 104,Mahalia,Sperling,Mahalia.Sperling@gmail.com,firefighter 105,Deirdre,Lemuela,Dierdre.Lemeuela@gmail.com,firefighter 106,Cyndie,Chem,Cyndie.Chem@gmail.com,police officer 107,Modestia,Engdah,Modestia.Engdah@gmail.com,doctor 108,Jaime,Corabella,Jaime.Corabella@gmaio.com,police officer 109,Gusty,Jehu,Gusty.Jehu@gmail.com,doctor
Now, to search for Nita in the following file we use: Syntax: grep {keyword/pattern} {filename}
grep Nita csv1
Output:
101,Nita,Eiser,Nita.Eiser@gmail.com,workerTo ignore the upper and lower case while searching we use:
Syntax: grep -i {keyword/pattern} {filename}
grep -i nita csv1
Output:
101,Nita,Eiser,Nita.Eiser@gmail.com,workerTo search everything except given pattern/keyword
Syntax: grep -v {keyword/pattern} {filename}
grep -v Nita csv1
Output:
id,firstname,lastname,email,profession 100,Mady,Cloris,Mady.Cloris@gmail.com,police officer 102,Ada,Rosette,Ada.Rosette@gmail.com,worker 103,Demetris,Teryn,Demetris.Teryn@gmail.com,worker 104,Mahalia,Sperling,Mahalia.Sperling@gmail.com,firefighter 105,Deirdre,Lemuela,Dierdre.Lemeuela@gmail.com,firefighter 106,Cyndie,Chem,Cyndie.Chem@gmail.com,police officer 107,Modestia,Engdah,Modestia.Engdah@gmail.com,doctor 108,Jaime,Corabella,Jaime.Corabella@gmaio.com,police officer 109,Gusty,Jehu,Gusty.Jehu@gmail.com,doctorTo print how many times (count) given keyword is present in the file
Syntax: grep -c {keyword/pattern} {filename}
grep -c “police officer” csv1
Output:
3To search for exact match of given keyword in a file
Syntax: grep -w {keyword/pattern} {filename}
grep -w Nit csv1
Output: {blank}
grep -w Nita csv1
Output:
101,Nita,Eiser,Nita.Eiser@gmail.com,workerTo print the line no. of matches of given keyword in a file
Syntax: grep -n {keyword/patttern} {filename}
grep -n Nita csv1
Output:
3:101,Nita,Eiser,Nita.Eiser@gmail.com,worker 12:NitasTo search a given keyword in multiple files
Syntax: grep {keyword/pattern} {file1} {file2}
1 2 3 4
#file1 Hi My name is Sheldon Welcome to grep tutorial
1 2 3 4
#file2 Hello My name is Leonard Welcome to grep tutorial
grep -n name file1 file2
Output:
file1:My name is Sheldon file2:My name is LeonardTo supress file names while searching a given keyword in multiple files
Syntax: grep -h {keyword/pattern} {file1} {file2}
grep -h name file1 file2
Output:
My name is Sheldon My name is LeonardTo search multiple keyword in a file
Syntax: grep -e {keyword 1/ pattern 1} -e {keyword 2 / pattern 2} {filename}
grep -w -e Nita -e Jaime csv1
Output:
101,Nita,Eiser,Nita.Eiser@gmail.com,worker 108,Jaime,Corabella,Jaime.Corabella@gmaio.com,police officerTo search multiple keywords in multiple files
Syntax: grep -e {keyword1} -e {keyword2} {file1} {file2}
grep -n -e Sheldon -n -e Leoanrd file1 file2
Output:
file1:2:My name is Sheldon file2:2:My name is LeonardTo only print filenames which matches given keyword
Syntax: grep -l {keyword} {file1} {file2}
grep -l -e Sheldon -e Leonard file1 file2 csv1
Output:
file1 file2To get the keyword/pattern from a file and match with another file
Syntax: grep -f {file with pattern} {file to search}
grep -n -f keyword.txt csv1 file1 file2
Output:
csv1:2:100,Mady,Cloris,Mady.Cloris@gmail.com,police officer csv1:10:108,Jaime,Corabella,Jaime.Corabella@gmaio.com,police officer file1:2:My name is Sheldon file2:2:My name is LeonardTo print the matching line which start with given keyword
Syntax: grep ^{keyword} {file}
grep -n ^101 csv1
Output:
5:103,Demetris,Teryn,Demetris.Teryn@gmail.com,workerTo print the matching line which ends with given keyword
Syntax: grep {keyword}$ {file}
grep -n doctor$ csv1
Output:
9:107,Modestia,Engdah,Modestia.Engdah@gmail.com,doctor 11:109,Gusty,Jehu,Gusty.Jehu@gmail.com,doctorSuppose we have 100 files in a directory (dirA) and we need to search a keyword in all the files
Syntax: grep -R {keyword} dirA/
grep -n -R -f keyword.txt .
Output:
./csv1:2:100,Mady,Cloris,Mady.Cloris@gmail.com,police officer ./csv1:10:108,Jaime,Corabella,Jaime.Corabella@gmaio.com,police officer ./file1:2:My name is Sheldon ./file2:2:My name is Leonard ./keyword.txt:1:Mady ./keyword.txt:2:Jaime ./keyword.txt:3:Starla ./keyword.txt:4:Selia ./keyword.txt:5:Sheldon ./keyword.txt:6:LeonardWe can use egrep command for the multiple keywords search
Syntax: egrep “key1|key2|key3” file
egrep -n “Leonard Sheldon Jaime” csv1 file1 file2 Output:
csv1:10:108,Jaime,Corabella,Jaime.Corabella@gmaio.com,police officer file1:2:My name is Sheldon file2:2:My name is LeonardIf you just want to search but don’t want to print on the terminal
Syntax: grep -q {keyword} {file}
grep -q Sheldon file1
Output: None
Here, the nothing is returned as output to the terminal. So it may be confusing to the user to know if the command is executed successfully or not. To figure this out, we see the exit status. We can see the exit status using
echo $?
command. If theecho $?
command returns0
then the last commad was executed successfully and if it returned1
then it was not executed successfully.For Example: Input: grep -q Sheldon file1 Output: None Input: echo $? Output:0
Input: grep -q Sheldon file2 Output: None Input: echo $? Output:1
If you wnat to supress error message
Syntax: grep -s {keyword} {file}
grep -qs nita csv