Linux Questions & Answers – Search Pattern

This set of Linux / Unix questions and answers focuses on string search operations & pattern matching commands. It will be useful for anyone learning Linux Basics, Essentials and/or Administration.

1. Which option of grep displays the line number as well?
a) -v
b) -l
c) -n
d) -E
View Answer

Answer: c
Explanation: None.

2. How can you search for blank line in a file?
a) $ grep “ “ file
b) $ grep “^$” file
c) $ grep [“ “] file
d) $ grep [^$] file
View Answer

Answer: b
Explanation: None.

3. Assuming the files fileA, fileB, fileAB, fileBC and fileABC, exist in a directory, which files match with the pattern file[ABC]?
a) fileA, fileB and fileABC
b) fileABC
c) fileA and fileB
d) fileAB, fileBC and fileABC
View Answer

Answer: c
Explanation: None.
advertisement
advertisement

4. What will be printed for the command below?
$ grep –c “^echo” abc
a) The count of lines that do not contain the pattern echo in file abc
b) The count of lines which begin with the pattern echo in file abc
c) The count of lines that ends with the pattern echo in file abc
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

5. Which one is used to select only one copy of the repeated lines?
a) uniq -u
b) uniq -d
c) uniq -c
d) uniq –I
View Answer

Answer: a
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Indicate the right option to search for BOB, Bob, BOb or BoB?
a) grep –i Bob files
b) grep ‘B[oO][bB]’ files
c) grep ‘[BOB]’ files
d) grep -v ‘Bob’ files
View Answer

Answer: b
Explanation: None.

7. Indicate the right option to search for anything not a letter or number
a) grep ‘^[a-zA-Z0-9]’
b) grep ‘[^a-zA-Z0-9]’
c) grep ‘[a-zA-Z0-9]’
d) None of the mentioned
View Answer

Answer: b
Explanation: None.
advertisement

8. How do you remove duplicate lines from the file foo using uniq?
a) sort foo | uniq –u
b) sort -u foo | uniq -d
c) sort foo | uniq –c
d) sort foo | uniq –I
View Answer

Answer: a
Explanation: None.

9. One of the entry of /etc/passwd file is shown below:
user1:x:1111:2222:Sanfoundry:/home/user1:/bin/bash
Which among the following will print userid and home dir in the following pattern “user1:/home/user1”
a) awk `{print $1 “:” $6}` /etc/passwd
b) awk `{print $1 “:” $7}` /etc/passwd
c) awk `{print $2 “:” $6}` /etc/passwd
d) awk `{print $2 “:” $7}` /etc/passwd
View Answer

Answer: a
Explanation: None.
advertisement

10. who | cut –d ” ” -f1
what is the ouput if the who command displays like this
user1 tty 0 1234
a) user1
b) user1 tty 0 1234
c) tty
d) tty 0 1234
View Answer

Answer: a
Explanation: None.

11. What is the command that can print lines of first file matching with second file?
a) printline
b) cmp
c) com
d) comm
View Answer

Answer: d
Explanation: None.

12. Which character to use to escape meaning of special characters in search operations?
a) []
b) ^
c) .
d) backslash (\)
View Answer

Answer: d
Explanation: We can use backslash character to escape the meaning of special characters in search. For example: grep ‘\.’ a.txt OR grep ‘\*’ a.txt OR grep ‘\^’ a.txt will search for fullstop (.), asterisk (*) or caret (^) characters respectively in the file a.txt

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.

Here’s the list of Best Books in Linux Commands & Shell Programming.
Here’s the list of Best Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.

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.