Home Grep Tutorial
Post
Cancel

Grep Tutorial

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,worker

  • To 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,worker

  • To 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,doctor

  • To print how many times (count) given keyword is present in the file

    Syntax: grep -c {keyword/pattern} {filename}

    grep -c “police officer” csv1

    Output:
    3

  • To 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,worker

  • To 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:Nitas

  • To 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 Leonard

  • To 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 Leonard

  • To 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 officer

  • To 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 Leonard

  • To only print filenames which matches given keyword

    Syntax: grep -l {keyword} {file1} {file2}

    grep -l -e Sheldon -e Leonard file1 file2 csv1

    Output:
    file1 file2

  • To 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 Leonard

  • To 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,worker

  • To 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,doctor

  • Suppose 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:Leonard

  • Syntax: egrep “key1|key2|key3” file

    egrep -n “LeonardSheldonJaime” 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 Leonard

  • If 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 the echo $? command returns 0 then the last commad was executed successfully and if it returned 1 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

This post is licensed under CC BY 4.0 by the author.