Unix Questions and Answers – grep command – 2

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “grep command – 2”.

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

Answer: d
Explanation: 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) { }
View Answer

Answer: c
Explanation: 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.
advertisement
advertisement

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

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

a) True
b) False
View Answer

Answer: a
Explanation: 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) %
View Answer

Answer: a
Explanation: 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.
advertisement

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

Answer: b
Explanation: 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???

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

Answer: d
Explanation: 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 $.

advertisement
^     - 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’.

$ grep “^2”  emp.lst

a) True
b) False
View Answer

Answer: a
Explanation: 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 –
View Answer

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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 )
View Answer

Answer: c
Explanation: 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.

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.