This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “sed command – 1”.
1. Which of the following commands is known as stream editor?
Explanation: sed is a multipurpose tool which performs the function of several filters together. This filter is derived from the ed (original UNIX editor). It performs non-interactive operations on a data stream. It uses instructions to act on a text for selecting lines, with an action to be taken.
2. What is the correct syntax for using sed?
a) sed options file(s)
b) sed options ‘action’
c) sed options ‘address action’ file(s)
d) sed ‘action’ file(s)
Explanation: sed is a multipurpose tool which performs the function of several filters together. It can even manipulate characters in the file. The syntax for using this tool is:
sed options ‘address action’ file(s)
3. Which one of the following command will be used for quitting after selecting 3 lines from file emp.lst?
a) sed -n 3 emp.lst
b) sed -i 1-3 emp.lst
c) sed ‘3q’ emp.lst
d) sed -n
Explanation: We can use line addressing feature of sed command by using ‘q’ along with the line number. For example, 3q will quit after selecting 3 lines from the file. Here ‘q’ is used for quitting.
4. Which of the following command is used with sed for outputting as well as printing the selected lines?
Explanation: Generally, ‘p’ is used for printing lines. However, this command behaves in a strange manner i.e. it both displays and prints the selected lines. So the selected lines will appear twice.
5. To suppress the behavior of ‘p’ command of sed, we use ____ option.
Explanation: When we use ‘p’, it behaves in a strange manner i.e. it both displays and prints the selected lines. So the selected lines will appear twice. To suppress this behavior, we use the -n option whenever we use the p command. For example,
//selects line number 1-3 from emp.lst $ sed -n ‘1,3p’ emp.lst
6. The command $ sed -n ‘$p’ emp.lst will display the last line.
Explanation: If we want to select the last line of the file, we can either provide the line number of the file or it is more convenient to use $ symbol.
7. Consider the following commands.
$ sed -n ‘1,2p’ emp.lst $ sed -n ‘3,$!p’ emp.lst
The output of both commands will be the same.
Explanation: We can also use the negate (!) symbol, which can be used for negating any action. For instance, selecting the first two lines is same as not selecting lines 3 through the end.
8. Which option is used with sed for using multiple instructions?
d) –f and -e
Explanation: Both -e and -f allows us to use multiple instructions with sed. For example, -e allows us to enter as many instructions as we want, each preceded by the option.
$ sed -n -e ‘1,2p’ -e ‘7,9p’ emp.lst
9. ____ option is used for taking instructions from a file.
Explanation: sed command is quite liberal and it provides a great freedom in using and repeating options. We can use the -f option to direct sed to take its instructions from the file.
$ sed -n -f instruct.txt emp.lst
10. To perform context addressing, we have to enclose the pattern in ____
a) double quotes
b) single quotes
c) / /
d) $ $
Explanation: The second form of addressing apart from relative addressing is context addressing. It allows us to specify one or two patterns to locate lines. The pattern must be bounded by a / on either side.
11. To select lines containing gupta and agarwal, which command will be used?
a) sed -n ‘/gupta/,/agarwal/p’ emp.lst
b) sed -n ‘/gupta/agarwal/p’ emp.lst
c) sed ‘/gupta | agarwal’p’ emp.lst
d) sed -n
Explanation: Context addressing allows us to specify one or two patterns to locate lines. To do so, a separate pair of context addresses by a comma.
12. To write selected lines, ____ is used with sed.
Explanation: For writing selected lines to a separate file irrespective of the way of addressing (relative or context), we can use w (write) command. For example,
$ sed -n ‘/director/w emp2.lst’ emp.lst //saves output to emp2.lst
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.