Usage of Practical grep commands examples useful in real world debugging in Linux

  • Grep the lines which contains some particular word
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep 'sahil' file1.log
i am sahil
sahil is a software engineer
  • Grep number of lines matched for a particular word in a file
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep -c 'sahil' file1.log
2

Another way :
[root@localhost playground]# grep 'sahil' file1.log | wc -l
2
  • Grep all the lines in which contains some word in a file with case insensitive
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep -i 'sahil' file1.log
i am sahil
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]#
  • Grep the lines in which either of two words are present in a file
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep 'sahil\|software' file1.log
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]#
  • Grep lines in which two words are present
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep 'sahil' file1.log | grep 'software'
sahil is a software engineer
  • Eliminate lines which contains some word
[root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep -v 'sahil' file1.log
hello
i am software engineer
Sahil is a software engineer

Eliminate case insensitively
[root@localhost playground]# grep -iv 'sahil' file1.log
hello
i am software engineer
[root@localhost playground]#
  • Matching the lines that start with a string
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep '^sahil' file1.log
sahil is a software engineer
  • Matching the lines that end with a string
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# grep 'engineer$' file1.log
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]#
  • Getting n number of lines after each match
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]#

[root@localhost playground]# grep 'hello' file1.log
hello

[root@localhost playground]# grep -A 1 'hello' file1.log
hello
i am sahil

[root@localhost playground]# grep -A 2 'hello' file1.log
hello
i am sahil
i am software engineer
  • Geeting n number of lines before each match
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]# grep 'i am sahil' file1.log
i am sahil
[root@localhost playground]# grep -B 1 'i am sahil' file1.log
hello
i am sahil
[root@localhost playground]# grep -B 2 'i am sahil' file1.log
hello
i am sahil
[root@localhost playground]#
  • Grep n lines after and m lines before every match
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer
[root@localhost playground]# grep -A 2 -B 1 'i am sahil' file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
[root@localhost playground]#
  • Grep some word in more than one file in current directory
[root@localhost playground]# cat file1.log 
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

[root@localhost playground]# cat file2.log
hello
i am sahil
i am tech blogger
Sahil is a tech blogger
sahil is a tech blogger

[root@localhost playground]# grep 'sahil' file1.log file2.log
file1.log:i am sahil
file1.log:sahil is a software engineer
file2.log:i am sahil
file2.log:sahil is a tech blogger
  • Grep some word in all files in current directory
[root@localhost playground]# grep  'sahil' *
file1.log:i am sahil
file1.log:sahil is a software engineer
file2.log:i am sahil
file2.log:sahil is a tech blogger
[root@localhost playground]#
  • Check how much lines matched in each file
[root@localhost playground]# grep -c  'sahil' *
file1.log:2
file2.log:2
file.log:0
  • Grep using regular expression —
[root@localhost playground]# cat file3.log 
time taken by api is 1211 ms
time taken by api is 2000 ms
time taken by api is 3000 ms
time taken by api is 4000 ms
time taken by api is 50000 ms
time taken by api is 123 ms
time taken by api is 213 ms
time taken by api is 456 ms
time taken by api is 1000 ms
[root@localhost playground]# grep -P '[0-9]{4} ms' file3.log
time taken by api is 1211 ms
time taken by api is 2000 ms
time taken by api is 3000 ms
time taken by api is 4000 ms
time taken by api is 50000 ms
time taken by api is 1000 ms

If want to get 5 digit number

[root@localhost playground]# grep -P '[0-9]{5} ms' file3.log
time taken by api is 50000 ms
  • Recursively grep in a directory and sub directories
[root@localhost playground]# grep -R 'sahil' .
./dir1/file.log:i am sahil
./dir1/file.log:sahil is a software engineer
./file1.log:i am sahil
./file1.log:sahil is a software engineer
./file2.log:i am sahil
./file2.log:sahil is a tech blogger
[root@localhost playground]#

--

--

SSE3 at Ameyo

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store