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.
                                  
[email protected]:~$ 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.

[email protected]:~$ 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.
                                  
[email protected]:~$ 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.
                                  
[email protected]:~$ 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.
                                  
[email protected]:~$  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.
                                  
[email protected]:~$ 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.
                                  
[email protected]:~$ grep -o "This" ok.txt
This
This

[email protected]:~$ grep -o -i "This" ok.txt
This
THIS
This


grep -o -b Displays only matched string with their positions.
                                  
[email protected]:~$ grep -o -b "This" ok.txt
0:This
78:This

grep -n Displays the line numbers while displaying the result of command.
                                  
[email protected]:~$  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.
                                  
[email protected]:~$ grep -r “This” *

grep -c To display total numbers of matched string.
[email protected]:~$ grep -c "This" ok.txt
2

[email protected]:~$ 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.
                                  
[email protected]:~$ 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.
                                  
[email protected]:~$ grep -e "This" -e "Guys" ok.txt
This is line 1.
Hello Guys, Welcome to includehelp.com
LINE3: This is line 3.







© https://www.includehelp.com (2015-2018), Some rights reserved.




Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.
Close