Unix Questions and Answers – Commands for Searching, Replacing and Substituting in Vi Editor

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Commands for Searching, Replacing and Substituting in Vi Editor”.

1. Which of the following symbols are used for searching a pattern in vi?
a) /
b) ?
c) %
d) / and ?
View Answer

Answer: d
Explanation: vi is extremely strong in searching for a pattern. Searching can be made both in forward and in reverse directions using / and ? respectively.

2. ____ searches for a first instance of a pattern in forwarding direction.
a) /
b) %
c) &
d) ?
View Answer

Answer: a
Explanation: We can use / symbol in command mode for searching a particular pattern in forwarding direction. The search begins forward to position the cursor on the first instance of the word. For example, if we are looking for the string “print” then we can use the following command:

/print  [Enter]        // searches forward

3. ____ searches for a first instance of a pattern in the backward direction.
a) /
b) %
c) &
d) ?
View Answer

Answer: d
Explanation: We can use ? symbol in command mode for searching a particular pattern in the backward direction. The search begins backwards to position the cursor on the first instance of the word. For example, if we are looking for the string “print” then we use the following command:

advertisement
advertisement
?print  [Enter]        // searches backward

4. Which command is used for repeating the search in the same direction?
a) n
b) N
c) nn
d) NN
View Answer

Answer: a
Explanation: For repeating a search in the same direction in which the previous search was made with either / or ? we can use ‘n’ command. In this way, we can press ‘n’ repeatedly to scan all instances of the pattern.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. Which command is used to reverse the direction of repeating the search?
a) n
b) N
c) nn
d) NN
View Answer

Answer: b
Explanation: ‘N’ reverses the direction pursued by ‘n’, which means that we can use it to retrace our search path.

advertisement

6. Substitution command is an ex-mode command.
a) True
b) False
View Answer

Answer: a
Explanation: vi offers a powerful feature, that is of substitution, which is achieved with the ex-mode substitute (s) command. It lets us replace a pattern in a file with some other pattern.

7. Which of the following symbol is used for substitution command?
a) n
b) N
c) s
d) S
View Answer

Answer: c
Explanation: For replacing a pattern with another pattern we use substitution command which is represented by ‘s’.
advertisement

8. Which of the following is a correct syntax for performing substitution on a file?
a) :address/source_pattern/target_pattern/flag
b) :address/target_pattern source_pattern/flag
c) :address/flag
d) :address
View Answer

Answer: a
Explanation: For performing substitution, the correct syntax is:
:address/source_pattern/target_pattern/flag
For example, if we want to perform substitution on line number 1 to 50, source pattern is ‘directory’ and target pattern is ‘member’ then,

:1,$s/directory/member/g

9. We cannot leave the flag field empty while performing the substitution.
a) True
b) False
View Answer

Answer: b
Explanation: There are many flags like ‘g’, ‘gc’ which are used as flags. For example, when we left the flag field empty in the following command:

:1,$s/directory/member

Then substitution will only be carried out for the first occurrence in each addressed line.

10. The target pattern is optional.
a) True
b) False
View Answer

Answer: a
Explanation: The target pattern specified is optional while performing the substitution. If the target pattern is left out, then instances of the source pattern in all addressed lines are deleted.

:1,$s/directory/ /g        // every instance of ‘directory’ will be deleted from each line.

11. Which of the following commands can be used to replace ‘professor’ with ‘director’ throughout the whole file?
a) 1,$s/professor/director/g
b) %s/professor/director/g
c) 1,$s/professor/director
d) 1,$s/professor/director/g and %s/professor/director/g
View Answer

Answer: d
Explanation: We can replace 1,$ with %. It will work same as 1,$. ‘g’ flag is used for global replacement.

12. Which of the following flag will be used for interactive substitution?
a) g
b) G
c) gc
d) cg
View Answer

Answer: c
Explanation: For selectively replacing a string we can use ‘gc’ flag. Here ‘c’ is used as a confirmatory parameter at the end. Each line is selected in turn and then waits for our response, whether to replace the selected pattern or not. A ‘y’ performs the substitution and any other response doesn’t.

13. Which one of the following commands will be used to delete all the instances of string ‘manager’ in file003?
a) 1,$s/manager/ /g
b) %s/manager/director/g
c) 1,$s/professor/director
d) %s/manager/ /
View Answer

Answer: a
Explanation: When the target pattern is left empty, then all the instances of the source pattern are deleted within the addressed lines in the file.

14. Which one of the following command is invalid?
a) /printf
b) ?scanf
c) :%/abd/mash/wc
d) :1,%/file01/file02/g
View Answer

Answer: d
Explanation:

/printf - searches for string “printf”
?scanf  	- searches for string “scanf”
:%/abd/mash/wc - replaces the string ‘abd’ to ‘mas
h’ using confirmatory flag
:1,%/file01/file02/g - invalid command

15. Search and replace operations can also use regular expressions for matching multiple patterns.
a) True
b) False
View Answer

Answer: a
Explanation: Both search and replace operations can make use of regular expressions for matching multiple patterns.

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.