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
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
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
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:
?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
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.
5. Which command is used to reverse the direction of repeating the search?
a) n
b) N
c) nn
d) NN
View Answer
Explanation: ‘N’ reverses the direction pursued by ‘n’, which means that we can use it to retrace our search path.
6. Substitution command is an ex-mode command.
a) True
b) False
View Answer
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
Explanation: For replacing a pattern with another pattern we use substitution command which is represented by ‘s’.
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
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
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
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
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
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
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
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
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.
- Check Computer Science Books
- Check MCA Books
- Check Unix Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs