250+ TOP MCQs on Grep Command and Answers

Unix Multiple Choice Questions on “grep command”.

1. Which of the following is not a subset of BRE (basic regular expression) character subset?
a) *
b) .*
c) ^$
d) ch+

Answer: d
Clarification: The basic regular expression character subset doesn’t contain ch+, as it is a part of the extended regular expression.

Symbols    Matches
*          - matches zero or more occurrences of the previous character.
.*         - nothing or any number of characters
^$         - lines containing nothing

2. Character class is used for matching a group of characters enclosed within a pair of _____
a) ( )
b) “ “
c) [ ]
d) { }

Answer: c
Clarification: A regular expression lets us specify a group of characters enclosed within a pair of rectangular brackets, [ ]. For example, [ra] matches either r or a.

3. The following command will match ‘Agarwal’, ‘agarwal’ and ‘agrawal’.

$ grep “[aA]g[ar][ar]wal” emp.lst

a) True
b) False

Answer: a
Clarification: The metacharacters [ and ] are used here to match all three agarwals. The character class [aA] matches the letter ‘a’ in both lowercase and uppercase. The model [ar][ar] matches any of the ‘aa’, ‘ar’,’ra’,’rr’.

4. Which of the following symbol is used for matching the immediately preceding character?
a) *
b) $
c) [ ]
d) %

Answer: a
Clarification: The asterisk (*) refers to the immediately preceding character. It can match the previous character which can occur any number of times or not at all. For example, g* can match a null string along with g, gg, ggg, gggg and so on.

5. Which symbol is used for matching a single character?
a) *
b) .
c) &
d) %

Answer: b
Clarification: A (.) matches a single character. The shell uses ? character to indicate that. The pattern g . . . will match a four character string beginning a ‘g’. The shell’s equivalent pattern is g???

  250+ TOP MCQs on File Ownership and Permissions and Answers

6. Which of the following symbols are used for matching a pattern at specified locations?
a) *
b) ^
c) $
d) ^ and $

Answer: d
Clarification: Most of the regular expression character are used for matching patterns, but there are two symbols that are used for matching pattern at beginning or end of a line. These symbols are ^ and $.

^     - for matching at the beginning of a line
$     - for matching at the end of line

7. The following command will match the lines beginning with ‘2’.

a) True
b) False

Answer: a
Clarification: The ^ is used for matching at the beginning of the line. So the above command will match the following lines:

2200| chanchal singhal | director | sales | 03/09/98 | 6700
2456| lalit chaudhary | director | marketing | 04/05/87 | 8200
2000 | barun sengupta | director | production | 09/09/78 | 7600

8. Which of the following symbols are a set of ERE (extended regular expressions)?
a) +
b) –
c) ?
d) + and –

Answer: d
Clarification: The ERE set includes two special characters, + and ? which are used in place of * to restrict the matching scope. They signify the following,

+     - matches one or more occurrences of the previous character
?    - matches zero or one occurrence of the previous character

9. Which option is used when we want to use an ERE with grep command?
a) -e
b) -i
c) -E
d) -i

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

10. Which of the following symbols are used for matching multiple patterns?
a) |
b) &
c) ( and )
d) | and ( and )

  250+ TOP MCQs on Handling Multiple Files and Buffers and Answers

Answer: c
Clarification: The | symbol is a delimiter of multiple patterns. Using it, we can match multiple patterns. For example, to match gupta and agarwal we can use the following command:

$ grep -E ‘gupta|agarwal’  emp.lst
Similarly to match sen gupta and das gupta use the following command:
$ grep -E (sen|das)gupta emp.lst.

Leave a Comment

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

Scroll to Top