Unix Questions and Answers – grep command – 1

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

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
View Answer

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

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

advertisement
advertisement

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
View Answer

Answer: d
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

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

advertisement

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

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

advertisement
$ 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
View Answer

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

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

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

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

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

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

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

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

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

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

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.