grep command in Linux with examples

Hello, everyone In this Article we are going to learn how we can search a string in Linux in a very simple and easy step-by-step approach.

grep command

The grep command is use to search a particular string from the file.

The grep is the best utility to find any string from any file.

If we want to find any word or line inside any file then we can use the grep command.

Before Jumping on the command we should know the syntax of the grep command

grep [options..][arguments..] filename

Argument and File name cannot skip but we can skip the options

1 ) How to search a word from the file

To search a particular word in a file then we can execute the below command. Here we are searching root word from /etc/passwd file

$ grep root /etc/passwd
Screenshot 1 36

2) How to search a word with the line number from the file

To display the output of the grep command with the line number then we can use the ( -n )option in the grep command.

$ grep -n root /etc/passwd
Screenshot 3 35

3)How to search case sensitive word from the file

To ignore the case-sensitive word from the file then we can use( -i )option in the grep command.

Here the Root keyword is in a capital letter still this can be searched. This will ignore the case sensitivity to the keyword that we are searching

$ grep -in Root /etc/passwd
Screenshot 4 33

4) How to find the word that is appearing at the starting of a line

If we only need a line where the root keyword is coming in 1st position, not in the mid or ending of the file then we need to execute below command.

We can add( ^ ) symbol in grep command

$ grep -in '^root' /etc/passwd
Screenshot 6 33

5) How to find the word that is appearing at the ending of a line

If we need a line where no login is coming in the last part of the line inside the file then we can add a ( $ ) dollar special symbol.

$ grep -in 'nologin$' /etc/passwd
Screenshot 7 27

6)How to View the Lines coming after searching the word

If the root is coming inline 1 & 10 but we need after 2 lines where the word is coming then this command will give after 2 lines where the root word is there. Then we can use the( -A )option.

$ grep -in -A2 root /etc/passwd
Screenshot 8 25

7) How to View the Lines coming before searching the word

If the root is coming inline 10 but we need before 2 lines where the word is coming then this command will give before 2 lines where root word is there. We can use ( -B ) option in the grep command.

$  grep -in -B 2 root /etc/passwd 
Screenshot 9 27

8) How to View the Lines coming after & before searching the word

If the root is coming in line 10 but we need after 2 lines where the word is coming and before 2 lines where the word is coming. We can use the ( -C ) option in the grep command.

$  grep -in -C2 root /etc/passwd 
Screenshot 10 25

9) How to find Multiple Keyword in a file

If we want to find multiple words then add | symbol and we have to use egrep command in this situation egrep is use for enhanced grep.

$ egrep -in ‘root|nologin’ /etc/passwd
Screenshot 11 26

We can also use grep command to filter log file

Suppose that we need November month logs then we can run the following command

# grep -in 'Nov' /var/log/messages
Screenshot 12 22

If we need a particular date log then we can execute this command

# grep -in 'Nov 10' /var/log/messages
Screenshot 13 20

How to run grep command in the directory

$ grep -Rin root /etc/ 

This command will search the root word in every file in /etc/ directory this will search we can use this when we do not know the exact filename

Screenshot 14 13

How to check version Of the grep command 

If we want to check what is the version of the grep command then we can execute the below command.

# grep --version
Screenshot 4 34

How to View the Most useful Options in the grep command 

If we want to know the most useful options with the grep command then we can execute the below command.

$ grep --help
Screenshot 5 34

Conclusion

If you want to learn Linux in the most simple language then please click on this link.

If you want to see the man page of grep command online then click on this link

This is the complete knowledge of how we can find the string using the grep command in the Linux kernel using the rm command if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on DevOps Articles.

Leave a Comment