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

Sahil Aggarwal
5 min readJul 18, 2021

--

In our Daily debugging we need to analyze logs files of various products . Reading those log files are not an easy task , it requires special debugging skills which can only be gained through experience or by god’s grace . Now while debugging we might need to extract some of data or we need to play with a log file which can not be done by just reading , there is need for commands .

There are many commands in linux which are used by debuggers like grep,awk,sed,wc,taskset,ps,sort,uniq,cut,xargs etc . . .

In this blog we will see usage of Practical grep commands examples useful in real world debugging in Linux . The examples which we will see in this blog are super basic but very useful in real life which a beginner should read to enhance the debugging skills .

Let’s Go to the Practical Part

  • 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 —

Suppose the content of files are as follows

[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

Now suppose we want to grep all the lines in which time taken by any api is more than 1 second or more than 1000 ms , it means it should have minimum 4 digit number .

Now grep command for this will be as follows :

[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]#

All above are basic use cases of grep . One can mix all the command options of grep to achieve the complex use cases and also one can also mix different grep commands using pipe operator to achieve complex use cases .

In future blogs i will explain some complex use cases and example how to achieve that using linux commands which can ease logs debugging .

Stay Tuned . . .

Originally published at http://hello-worlds.in on July 18, 2021.

--

--