-->

"grep" command interview Questions

Posted by Admin on
We have three files in our current directory as below :

Newfile.txt

This is ishant gaurav
I love to play cricket
I am bit confused now a days.
Life is all about improving the things


Newfile1.txt

Hi
This is fun working as a tester
Its not the work which makes u better
Its your attitude which take you different height

Newfile2.txt

In this competitive world
You always need to keep improving your skills
You cant stick with one thing and hope
Everyone out there is working very hard .
First of all you mjst believe in yourslef that you can do it


Q1 : Write a command to print the lines that has the the pattern "this" in all the files in a particular directory?

Command : grep "this" *

sh-4.2# ls                                                                    
Newfile.txt  Newfile1.txt  Newfile2.txt  main.sh                              
sh-4.2# grep this *                                                           
Newfile2.txt:In this competitive world                                        

This will print all the lines in all files that contain the word “this” along with the file name. If any of the files contain words like "THIS" or "THis"or anything different from "this", the above command would not print those lines.


Q2 :Write a command to print the lines that has the word "july" in all the files in a directory and also suppress the filename in the output.

Command : grep -h this *

-h will remove the file from the result in front of every line .

sh-4.2# grep -h this *                                                        
In this competitive world                                                     


Q3 : Write a command to print the lines that has the word "july" while ignoring the case.

Command : grep -i this *

The option i make the grep command to treat the pattern as case insensitive.

sh-4.2# grep -i this *                                                        
Newfile.txt:This is ishant gaurav                                             
Newfile1.txt:This is fun working as a tester                                  
Newfile2.txt:In this competitive world                                        


Q4 :Write a Unix command to display the lines in a file that do not contain the word "this"?

Command : grep -v this Newfile2.txt

sh-4.2# grep -v this Newfile2.txt                                             
You always need to keep improving your skills                                 
You cant stick with one thing and hope                                        
Everyone out there is working very hard .                                     
First of all you mjst believe in yourslef that you can do it                  

The '-v' option tells the grep to print the lines that do not contain the specified pattern.

Q5 : Write a command to print the file names in a directory that has the word "this"?

Command : grep -l this * 

sh-4.2# grep -l this *                                                        
Newfile2.txt                                                                  

The '-l' option make the grep command to print only the filename without printing the content of the file. As soon as the grep command finds the pattern in a file, it prints the pattern and stops searching other lines in the file.


Q6 : Write a command to print the file names in a directory that does not contain the word "this"?

Command : grep -L this *


sh-4.2# grep -L this *                                                        
Newfile.txt                                                                   
Newfile1.txt                                                                  

The '-L' option makes the grep command to print the filenames that do not contain the specified pattern.

Q7 : Write a command to print the line numbers along with the line that has the word "this"?

Command : grep -n this *

sh-4.2# grep -n this *                                                        
Newfile2.txt:1:In this competitive world                                      

The '-n' option is used to print the line numbers in a file. The line numbers start from 1

Q8 : Write a command to print the lines that starts with the word "^Y"?

Command : grep '^Y' NewFile2.txt

sh-4.2# grep '^Y' Newfile2.txt                                                
You always need to keep improving your skills                                 
You cant stick with one thing and hope                                        

Q9 : Write a command to print the lines which end with the word "hard"?

Command : grep 'hard$' Newfilee.txt

The '$' symbol specifies the grep command to search for the pattern at the end of the line.

Q10 : Write a command to select only those lines containing "is" as a whole word?

Command : grep -w 'is' *

sh-4.2# grep -w 'is' *                                                        
Newfile.txt:This is ishant gaurav                                             
Newfile.txt:Life is all about improving the things                            
Newfile1.txt:This is fun working as a tester                                  
Newfile2.txt:Everyone out there is working very hard .  

The '-w' option makes the grep command to search for exact whole words. If the specified pattern is found in a string, then it is not considered as a whole word. For example: In the string "mikejulymak", the pattern "is" is found. However "Ishant" is not a whole word in that string.


Please comment if you find the above post interesting or if you find any mistake.

No comments:

Post a Comment