Practical uses of Grep

1. Suppose you have multiple files in the system with the same first name and last name but the middle name is different (example xxx-4.deb, xxx5.deb). You have to find out the specific file (xxx6.deb). How will you perform this task?


Ans: Grep is a powerful file pattern searcher tool, which helped in searching the specific file.

Grep stand for Global regular expression print.
Command : dpkg -l | grep -i xxx6.deb
dpkg -l list out all the files and grep -i filter the output.

Benefits : Grep saved the user time while searching the file.

Pictorial Representation:




As you can see in above diagram, grep find out the file which you are looking for.

2. Suppose you are working as an Apache administrator in XYZaa company. There is some issue occur in the Apache server. Senior Engineer asked you to send the content of configuration file without commented lines. How will you perform this task?

Ans : grep -v '#' filename

Above command will print the content without #. -v is an option which is used to display the opposite result.

Pictorial Representation:





Grep will print this output.


3. if you want to display the string after the specific string which you have searched.Apart from this, you also have to save the output in the file. How will you do this ?

Ans: ifconfig | grep -A1 eth0 >> file.txt

It will display one line of the content of string after the result string.In above example, it will save the content or output in the file (file.txt) after searching the eth0 string.

Pictorial Representation:





As you can see in the above diagram, first we have filter the output using grep command and later we have saved the output in the file.txt using >> symbol.

4. Suppose after saving the document you will come to know that there is a string spelling mistake in the file and you want to count the number of specific string occurrence inside the file. How will you do this?

Ans: grep -c “count” filename

-c is an option which is used to count the occurrence of the string appear in the file.

Pictorial Representation:




5. Suppose you have saved the server credentials inside the directory but forget where you have saved exactly in the directory. If you want to search server credentials inside the directory and their respective sub directories. How will you find out?

Ans: grep -r “Server” Directoryname

-r is an option which is used to find the string in the current directory as well as inside the sub-directory.
Pictorial Representation:




Note: If you are looking for Complete RHEL-8 Book, Please click on below link:

https://amzn.to/2sW554D

6. Suppose if you have to print the line number along with the string pattern. How will you perform this task?

Ans : grep -n “stringpattern” filename

-n is an option which is used to display line number along with the string.

Pictorial Representation:

In the above diagram , grep filter the output and print the string ie is 1.stringpattern & 3.stringpattern.



7. Is it possible to use grep without any option to find string pattern in the file? If yes please explain.

Ans: \|

grep 'a\|b' filename.

Above command search for a and b string. It will print if it found either a or b.

Pictorial Representation:





In the above diagram grep filter the output and print a as well as b.

8. Suppose if you have to check the log of the specific date of the Linux server. How will you perform the task using grep?

Ans: caret symbol (^)

grep “^ 7 nov 2016” /var/log/messages

Pictorial Representation:


In above diagram, grep filter the output and print 7 nov 2016 system rebooted.

9. Suppose if you are facing issue with your hard drive which is failing frequently from last one week. You are eager to know which day it failed. How will you get the log as per your requirement?

Ans : grep “Failed.$” /var/log/messages

Pictorial Representation:





In above diagram as you can see grep filter the output as per our requirement.

10. Suppose if you have to search a string pattern like abdf in both cases i.e is small and the capital letter in the file.How will you find out?

Ans : grep -i 'abdf' filename

i option is used to ignore the case-sensitive. If find abdf or ABDF, it will print both.

Pictorial Representation:






In above diagram grep print both ie is abdf and ABDF.

Note: If you are looking for Complete RHEL-8 Book, Please click on below link:

https://amzn.to/2sW554D


Comments

Post a Comment

Popular posts from this blog

Python & Shell Scripting Real Time Course Book & Videos

Top Five Devops Technical Interview QA Books

Linux-Command Hands-On (DF)