Unix Questions and Answers – Filtering Commands – 1

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

1. Filter’s are a category of commands that take the advantage of shell redirection feature.
a) True
b) False
View Answer

Answer: a
Explanation: Filters are defined as a category of commands that takes the advantage of shell’s redirection features. A filter is capable of reading standard input and writing to standard output. The piping mechanism is an example of a filter which lets the standard output of one filter to serve as standard input to another.

2. Which command is used for preparing a file for printing?
a) cd
b) cat
c) print
d) pr
View Answer

Answer: d
Explanation: The pr command is used for preparing a file by adding suitable headers, footers and formatted text. This command is used with a filename as an argument. For example,

$ pr  dept.lst
May  06  10:38  1998  dept.lst  Page 1
01:accounts:6213
02:admin:5423
03:marketing:6521
04:personnel:2365
 
.....blank lines.......

3. pr command adds ____ lines of margin at the top and bottom.
a) 5
b) 4
c) 2
d) 1
View Answer

Answer: a
Explanation: pr command is used for preparing a file for printing by adding suitable headers, footers and formatted text. It adds five lines of margin at the top and five at the bottom. The header shows the date and time of last modification of the file along with filename and page number.
advertisement
advertisement

4. Which option is used with pr command for printing multi-columnar output?
a) -p
b) -k
c) -d
d) -pr
View Answer

Answer: b
Explanation: -k option is used with pr command (where k is an integer) for printing in k columns. For example, if a file contains a series of 12 numbers then we can divide the output into 3 columns in this way:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ dept.lst | pr -3
May  06  10:38  1998  dept.lst  Page 1
 
1    5    9
2    6    10
3    7    11
4    8    12

advertisement

5. Which option is used with pr command to suppress the header and footers?
a) -p
b) -k
c) -t
d) -n
View Answer

Answer: c
Explanation: We can use the -t option with pr command if we want to suppress the headers and associated which are displayed with the output of pr command. For example,

$ dept.lst | pr -t  -5        // no headers and footers; multi-columnar output
1    5    9
2    6    10
3    7    11
4    8    12

advertisement

6. Which option is used with pr command to display output along with line numbers?
a) -d
b) -n
c) -o n
d) -t
View Answer

Answer: b
Explanation: pr command offers many options which are used for producing the output in just the format we need. For example,
-d double spaces input
-n numbers lines, helps in debugging code
-o n offsets lines by n spaces, increases left margin of the page

$ pr  -t  -n  -d  -o  10  dept.lst
    1    01:accounts:6213
    2    02:admin:5423
    3    03:marketing:6521
    4    04:personnel:2365

7. -h option is used with pr command to add a header of our choice.
a) True
b) False
View Answer

Answer: a
Explanation: If we are not using the -t option with pr command then we can use the -h option to add a header of our choice. This option is followed by the header string. For example,

$ pr  -h “Department List”  dept.lst

8. ___ operator is used with pr command to start printing from a specific page.
a) –
b) #
c) +
d) &
View Answer

Answer: c
Explanation: There’s an option available with pr command which lets the user to print from a specific page number. This function is performed by prefixing a + with the page number. For example, to print from page number 5, use the following command:

$ pr +5  dept.lst

9. Which command is used for displaying the beginning of a file?
a) pr
b) head
c) begin
d) lp
View Answer

Answer: b
Explanation: The head command is used for displaying the top of the file. As the name suggests, it displays the beginning of the file which is specified as an argument to the command. For example,

$ head emp.lst

10. By default, how many lines are displayed using the head command?
a) 5
b) 10
c) 4
d) 20
View Answer

Answer: b
Explanation: The head command, as the name suggests displays the top of the file. When this command is used without any option, it displays the first ten lines of the file.

11. Which option is used with the head command to specify line count to display?
a) -a
b) -h
c) -n
d) -o
View Answer

Answer: c
Explanation: We can use the -n option with the head command to specify a line count and display a specific number of lines of the file. -n option is followed by an integer value which donates the number of lines to be displayed. For example,

$ head -n  3 emp.lst            // displays first 3 lines of emp.lst

12. ___ command is used for displaying the end of the file.
a) head
b) tail
c) lp
d) pr
View Answer

Answer: b
Explanation: Complementing the head command, the tail command is used to display the end of the file. It provides an additional feature of addressing lines. By default, it displays the last ten lines of the file.

13. We can also use -n option with tail command.
a) True
b) False
View Answer

Answer: a
Explanation: Alike head command we can also use the -n option with the tail command to specify the line count to be displayed from the end of the file. For example,

$ tail  -n  3  emp.lst            // displays last three lines

14. Which symbol is used with the tail command to print the file from the selected line?
a) +
b) –
c) %%
d) ^
View Answer

Answer: a
Explanation: We can also address lines from the beginning instead of the end. The +count option allows us to do that, where count represents the line number from where the selection should begin. For example,

$ tail +11 emp.lst        // start printing from 11th line onwards

15. Which of the following command is incorrect?
a) pr dept.lst
b) head -n 5 emp.lst
c) tail -o +5 emp.lst
d) tail -3 emp.lst
View Answer

Answer: c
Explanation: The first command will prepare a file for printing by adding header, footers and formatted text. The command head -n 5 emp.lst will display the first 5 lines of emp.lst while the command tail -3 emp.lst will display the last 3 lines of emp.lst

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.