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
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
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.
3. The following command will match ‘Agarwal’, ‘agarwal’ and ‘agrawal’.
$ grep “[aA]g[ar][ar]wal” emp.lst
a) True
b) False
View Answer
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
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.
5. Which symbol is used for matching a single character?
a) *
b) .
c) &
d) %
View Answer
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
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 $.
^ - 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
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
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
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
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 sengupta and dasgupta 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.
- Check Unix Books
- Check MCA Books
- Check Computer Science Books
- Practice MCA MCQs
- Practice Computer Science MCQs