250+ TOP MCQs on Filtering Commands and Answers

Unix Quiz on “Filtering Commands”.

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

Answer: c
Clarification: 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

Answer: d
Clarification: 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,

$ 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

Answer: c
Clarification: 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

Answer: c
Clarification: 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,

$ 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

Answer: b
Clarification: 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.

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

Answer: c
Clarification: 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

Answer: a
Clarification: 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

Answer: b
Clarification: 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

Answer: d
Clarification: 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

Answer: b
Clarification: 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

Answer: a
Clarification: 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

Answer: b
Clarification: 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

Answer: a
Clarification: 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

Answer: a
Clarification: 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.

Leave a Reply

Your email address will not be published. Required fields are marked *