250+ TOP MCQs on Grep Command and Answers Quiz

Unix Multiple Choice Questions on “grep command”.

1. Which one of the following command is used for searching for a pattern in one or more file(s)?
a) cd
b) cp
c) paste
d) grep

Answer: d
Clarification: UNIX has a special family of commands for handling search requirements, and the principal member of this family is the grep command. This command scans its input for a pattern and displays the lines containing the pattern, the line numbers or filenames containing the pattern.

2. Which one of the following is the correct syntax for grep command?
a) grep options filename(s)
b) grep options pattern
c) grep pattern filename
d) grep options pattern filename(s)

Answer: d
Clarification: grep command is used to search a file for a pattern and display both matching and non-matching lines. The syntax for using grep command is:

grep  options  pattern  filename(s)

3. Which one of the following command will be used for searching “director” in emp.lst?
a) grep “director”
b) grep -v “director” emp.lst
c) grep -director emp.lst
d) grep “director” emp.lst

Answer: d
Clarification: Because grep command is also a filter, it can search it’s standard input for the pattern. For example, the command grep “director” emp.lst will search the file emp.lst for the pattern “director” and will display the lines containing this pattern.

4. When the pattern is not found in a file, grep command silently returns the prompt.
a) True
b) False

Answer: a
Clarification: grep command is also used as a filter for searching a pattern in a file. When the pattern is not found in the file, this command silently returns the prompt without displaying any diagnostic messages. For example,

$ grep “executive” emp.lst    
$ _                        // executive not found

5. grep command can be used for searching a pattern in more than one file.
a) True
b) False

Answer: a
Clarification: When we use grep command with multiple filenames, it displays the filename along with the output. For example, when we search “director” in emp1.lst emp2.lst then the following result will be displayed,

$ grep  “director” emp1.lst  emp2.lst
emp1.lst:1006| chanchal singhal | director | sales | 03/09/98 | 6700
emp2.lst:6521| lalit chaudhary | director | marketing | 04/05/87 | 8200
emp1.lst:4358 | barun sengupta | director | production | 09/09/78 | 7600

6. If there are special characters in a pattern, then we’ve to enclose them in ______
a) single quotes
b) double quotes
c) without any quotes
d) all quotes

Answer: b
Clarification: We’ve to quote the pattern in double quotes when it contains multiple words or special characters else they will be interpreted in some other way by the shell. If the pattern doesn’t contain multiple words, then there is no need for quoting the patter in any quotes. If the pattern contains only multiple words, then we can quote the pattern in single quotes also.

7. Which option is used with grep command for ignoring the case in pattern searching?
a) -a
b) -v
c) -i
d) -e

Answer: c
Clarification: When we want to search a pattern using grep command and we want to ignore the case or we are not sure of the case, we’ve to use the -i option. This option ignores the case the pattern matching.

$ grep  -i  ‘agarwal’  emp.lst
3564| sudhir Agarwal | executive |personal | 06/07/47 |7500

8. Which option is used with grep command for deleting lines?
a) -v
b) -e
c) -a
d) -i

Answer: a
Clarification: grep can play an inverse role; the -v (inverse) option selects all lines except those containing the pattern. For example, the following command.

9. Which option is used for displaying the line numbers containing the pattern along with lines?
a) -v
b) -i
c) -e
d) -n

Answer: d
Clarification: The -n (number) option displays the line numbers containing the pattern, along with the lines. For example,

$ grep  ‘director’ emp1.lst 
3:1006| chanchal singhal | director | sales | 03/09/98 | 6700
5:6521| lalit chaudhary | director | marketing | 04/05/87 | 8200
9:4358 | barun sengupta | director | production | 09/09/78 | 7600

10. ______ option counts the number of lines containing the pattern?
a) -c
b) -i
c) -e
d) -n

Answer: a
Clarification: The -c option when used with grep command, counts the number of lines containing the pattern (which is not same as the number of occurrences of the pattern). For example,

$ grep  -c  ‘director’  emp.lst
4                // 4 lines contain the pattern ‘director’

11. Which option displays only the filename containing the pattern?
a) -i
b) -n
c) -e
d) -l

Answer: d
Clarification: The -l (list) option displays only the names of files containing the pattern. For example, the following command will display the filenames having an extension .lst and containing the pattern ‘director’:

$ grep  -l  ‘director’ *.lst
Design.lst
Emp.lst
Emp1.lst

12. _____ option is used when we need to match multiple patterns in a single invocation of grep command?
a) -a
b) -e
c) -n
d) -i

Answer: b
Clarification: grep provides an option (-e) which is used when we want to match multiple patterns in a single invocation of the command. For example, the following command will match three agarwals:

$ grep  -e “agarwal”  -e “aggarwal”  -e “ agrawal”  emp.lst

13. For taking patterns from a file, -f option is specified with grep command.
a) True
b) False

Answer: a
Clarification: If we want to search a single pattern or more than one pattern, then we can place them into a file with one pattern per line and after that, we can take the pattern for the grep command from the same file using -f option. For example,

$ grep -f  pattern.lst  emp.lst        // pattern.lst contains 3 different patterns

14. POSIX identifies regular expressions as belonging to ____ categories.
a) 3
b) 2
c) 4
d) 5

Answer: b
Clarification: POSIX identifies regular expressions as belonging to two categories i.e. regular and extended.

15. grep command supports both extended and regular expressions.
a) True
b) False

Answer: a
Clarification: grep command supports both categories of regular expressions. It supports basic regular expression by default and extended regular expression with -E option.

Leave a Reply

Your email address will not be published. Required fields are marked *