Home » Linux

Linux Terminal Commands - grep command

Working with grep command (searching string in file)

grep command is used to searching string(s), regular expression in the file(s). There are many options with this command, which help you to make your searching perfect.

command description
grep "string"   file The primary task of this command is used to search string(s) with in the file.
This command displays the all lines which has the same string.
                                  
ih@linux:~$ cat ok.txt
This is line 1.
THIS IS LINE 2.
Hello Guys, Welcome to includehelp.com
LINE3: This is the line 3.
Enjoy learning online tutorials.

ih@linux:~$ grep “This” ok.txt
This is line 1.
LINE3: This is the line 3.
grep -i -i is used to case insensitive searching and display all lines which has the same string but ignoring the case.
                                  
ih@linux:~$ grep -i “This” ok.txt
This is line 1.
THIS IS LINE 2.
LINE3: This is the line 3.
grep -A   n Displays n lines after the string matched.
                                  
ih@linux:~$ grep -A 2 "1" ok.txt
This is line 1.
THIS IS LINE 2.
Hello Guys, Welcome to includehelp.com

grep -B   n Displays n lines before the string matched.
                                  
ih@linux:~$  grep -B 2 "Enjoy" ok.txt
Hello Guys, Welcome to includehelp.com
LINE3: This is line 3.
Enjoy learning online tutorials.

grep -C   n Displays n lines before & after the string matched.
                                  
ih@linux:~$ grep -C 2 "Guys" ok.txt
This is line 1.
THIS IS LINE 2.
Hello Guys, Welcome to includehelp.com
LINE3: This is line 3.
Enjoy learning online tutorials.

grep -o Displays only matched string.
                                  
ih@linux:~$ grep -o "This" ok.txt
This
This

ih@linux:~$ grep -o -i "This" ok.txt
This
THIS
This


grep -o -b Displays only matched string with their positions.
                                  
ih@linux:~$ grep -o -b "This" ok.txt
0:This
78:This

grep -n Displays the line numbers while displaying the result of command.
                                  
ih@linux:~$  grep -n "This" ok.txt
1:This is line 1.
4:LINE3: This is line 3.

grep -r To search string in all files in the same and sub directories.
                                  
ih@linux:~$ grep -r “This” *

grep -c To display total numbers of matched string.

ih@linux:~$ grep -c "This" ok.txt
2

ih@linux:~$ grep -c -i "This" ok.txt
3
grep -v To inverts the match, when you do not want to display those lines which has the matched strings, use this option.
                                  
ih@linux:~$ grep -v "This" ok.txt
THIS IS LINE 2.
Hello Guys, Welcome to includehelp.com
Enjoy learning online tutorials.
grep -e To search multiple strings.
                                  
ih@linux:~$ grep -e "This" -e "Guys" ok.txt
This is line 1.
Hello Guys, Welcome to includehelp.com
LINE3: This is line 3.






Comments and Discussions!










Copyright © 2023 www.includehelp.com. All rights reserved.