Unix Questions and Answers – Filtering Commands – 2

This set of Unix Quiz focuses on “Filtering Commands – 2”.

1. Which option is used with the tail command for extracting bytes instead of lines?
a) -f
b) -n
c) -c
d) -a
View Answer

Answer: c
Explanation: tail command supports the -c option followed by a positive integer or just an integer depending on whether the extraction is performed relative to the beginning or end of the file. For example,

$ tail -c   511 foo   // extracts last 511 bytes from foo
$ tail -c  +512 foo   // extracts everything after skipping the first 511 bytes

2. To extract specific columns from a file, ____ command is used.
a) tail
b) head
c) pr
d) cut
View Answer

Answer: d
Explanation: To extract specific columns from a file, we can use cut command followed by -c option and list of column numbers, delimited by a comma. For example,

advertisement
advertisement
$ cut  -c  6-22,24-32  emp.lst

3.The expression cut -c -3 emp.lst will cut columns number _____
a) 3
b) 1
c) 1-3
d) 0
View Answer

Answer: c
Explanation: cut command is used for cutting specific columns. Cut command uses a special feature for selecting a column from the beginning and up to end of a line. For example,

$ cut -c -3 emp.lst            // extracts column 1-3
$ cut -c  10-  emp.lst        // extracts from column number 10 to end of line

4. ____ option is used with the cut command for cutting fields.
a) -c
b) -n
c) -f
d) -a
View Answer

Answer: c
Explanation: Sometimes we need to extract specific fields from a file instead of columns. For this purpose, -f option is used with cut command followed by an integer number representing the field. For example,

advertisement
$ cut  -f   2,3  emp.lst        // cut field number 2 and 3 from emp.lst

5. What is the default delimiter used by the cut command for cutting fields?
a) space
b) tab
c) double tab
d) underscore
View Answer

Answer: b
Explanation: To extract useful data from a file, we use the cut command to cut fields rather than columns. The cut command uses tab as the default delimiter to cut fields.
advertisement

6. Which option is used with the cut command for cutting field base on the value of our specified delimiter?
a) -a
b) -f
c) -d
d) -e
View Answer

Answer: c
Explanation: By default, the cut command uses tab as the default delimiter but we can specify our own delimiter using -d option. For example,

$ cut  -d “ “ -f 1  emp.lst        // space is the delimiter
Root
Kumar
Sharma
Project

7. We have to specify whether we want to cut fields or columns while using cut command.
a) True
b) False
View Answer

Answer: a
Explanation: Before using cut command, we have to keep in mind that it is necessary to specify that whether we want to cut fields or columns. This can be specified using -f and -c option respectively.

8. The following command will produce an error.

$ cut -d “|”  -f  1,4  emp.lst

a) True
b) False
View Answer

Answer: b
Explanation: Since cut uses the tab as the default field delimiter but we can also work with a different delimiter. Above command uses | (pipe) as the delimiter but we have to escape | in “ ” to prevent the shell from interpreting it as a pipeline character.

9. Which one of the following commands is incorrect?
a) $ cut -d “|” -f 1,4 emp.lst
b) $ cut -c -3 emp.lst
c) $ tail -c 511 foo
d) $ tail -c foo
View Answer

Answer: d
Explanation: tail(1) command is used for extracting bytes instead of lines while cut(1) command is used for extracting columns and fields. But if we are using tail(1) command, we have to specify an integer value after the -c option, otherwise, the command will fail.

10. Which command is used for pasting files?
a) cut
b) paste
c) tail
d) head
View Answer

Answer: b
Explanation: Whatever we’ve cut using cut command can be pasted back using paste command. This command is useful when we want to view files side by side by pasting them together. For example,

$ paste file01  file02            // contents of both files will be pasted together
Abd    01    98%
Mash    03    76%
Ryan    10    65%

11. Whatever we have cut using cut command can be pasted back using paste command but vertically.
a) True
b) False
View Answer

Answer: a
Explanation: Whatever we’ve cut using cut command can be pasted back using paste command. This command is useful when we want to view files side by side by pasting them together. But this command pastes vertically rather than horizontally.

12. paste command uses space as the default field delimiter.
a) True
b) False
View Answer

Answer: b
Explanation: Like cut command paste command also uses tab as the default field delimiter but we can use our own field delimiter using -d option with paste command.

13. _____ option is used with paste command if we want to specify our own delimiter.
a) -d
b) -c
c) -a
d) -e
View Answer

Answer: a
Explanation: Whatever we’ve cut using cut command can be pasted back using paste command. paste command also uses tab as the default field delimiter but we can use our own field delimiter using -d option with paste command. For example,

$ paste -d “|” file01  file02        // | is used as delimiter
Abd    |01 |98%
Mash |03 |76%
Ryan  |10 |65%

14. Which option is used with paste command for joining lines?
a) -s
b) -c
c) -a
d) -e
View Answer

Answer: a
Explanation: The -s option joins the line in the same way the vi editor’s J does. Using this option on a file containing multiple lines will join all of these lines into a single one.

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of Unix for Quizzes, 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.