This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Sort Command”.
1. ____ command is used for sorting a file on specified fields.
Explanation: Sorting is the ordering of data in ascending or descending order. The sort command is used for ordering a file. Like cut command, sort also identifies fields and perform sorting on specified fields. To sort any file, just provide the name of the file as an argument with sort command.
$ sort emp.lst
2. By default, sort command reorders lines in ASCII collating sequence.
Explanation: By default, sort reorders lines in ASCII collating sequence i.e. whitespaces first, then numerals, uppercase letters and at last lowercase letters. But we can change this default sorting sequence using a certain sequence.
3. Sort command uses ____ as default field delimiter.
b) single space
c) double tab
d) one or more continuous spaces
Explanation: Unlike cut and paste command which uses tab as the default field delimiter, sort command uses one or more contiguous spaces as the default field separator.
4. Which option is used with sort command to specify the field delimiter?
Explanation: sort command uses one or more contiguous spaces as the default field separator but we can specify our own field delimiter using the option -t. For example, to perform sorting using | as the delimiter, use the following command:
$ sort -t “|” file01 1243|Abd |01 |98% 2345|Ryan |03 |76% 2390|Mash |10 |65%
5. Which option is used for sorting a file according to primary key?
Explanation: sort command provides an option for sorting file according to our specified field. For this purpose, we have to use -k along with the field number (based on which we want to perform sorting). For example, to perform sorting on second field (name):
$ sort -t “|” -k 2 file01 1243|Abd |01 |98% 2345|Mash |10 |65% 2390|Ryan |03 |76%
6. The sort order can be reversed using ___ option.
Explanation: The sort order can be reversed using -r option. For example, the following sequence reverse the order,
$ sort -t “|” -k 2r file01 2390|Ryan |03 |76% 2345|Mash |10 |65% 1243|Abd |01 |98%
7. We can perform sorting on secondary key also using sort command.
Explanation: We can sort on more than one fields i.e. we can provide a secondary key to sort. For example, if the primary key is the third field and the secondary key is the second field, then we have to specify for every -k option, where the sort ends. This is done in the following manner:
$ sort -t “|” -k 3,3 -k 2,2 shortlist
This command will sort the file according to the second and third field. 3,3 indicates that sorting starts and end on the third field.
8. We cannot perform sorting on columns using sort command.
Explanation: We can also specify a character position within a field to the beginning of sort. For example, if we have to sort salary field then we can specify in the form -k m,n where n is the character position in mth field.
$ sort -t “|” -k 6.1,6.2 shortlist // sort according to the first two digits of salary.
// here salary is the 6th field.
9. Which option is used when we’ve to sort files containing only numbers?
Explanation: When sort command acts on a numeral, strange things can happen if a file contains only numerals. So we have to use the -n option with sort command. For example,
$ sort numfile // without using -n 10 2 27 4 $ sort -n numfile // using -n 2 4 10 27
10. _____ option is used with sort command for removing repeated lines.
Explanation: sort provides an option i.e. -u (unique) which lets us remove repeated lines from a file. We can use this option with sort command to find unique occurrences in the file. For example,
$ cut -d “|” -f3 emp.lst | sort -u | tee design.lst Above command will cut out the designation field from emp.lst, after that the output will be piped to sort command to find out the unique designations in the file. The output produced will be like: Chairman d.g.m director executive g.m. manager
11. Which option is used by the sort command to redirect the output to a specified file?
Explanation: Even though we can redirect the output of sort to a file, we can also use its -o option for specifying the output file. For example,
$ sort -o sortedlist -k 3 shortlist // output stored in sortedlist
12. The name of the input and output files cannot be same while using sort command.
Explanation: Even though we can redirect the output of sort to a file, we can also use its -o option for specifying the output file. And the interesting fact is, the name of the input and output file can be same. For example,
$ sort -o sortedlist -k 3 shortlist // output stored in sortedlist $ sort -o shortlist shortlist // output stored in the same file
13. To check whether the file has actually been stored in the default order, we can use ____ option.
Explanation: sort command provides an option with the use of which we can check whether the file has actually been stored in the default order. For example,
$ sort -c shortlist _ // file is sorted
14. Which option is used with sort command to sort multiple files collectively?
Explanation: When we use sort command with multiple filenames as arguments, it concatenates all of them and sorts them collectively. -m option usually makes the performance to suffer. For example, to sort three files, use the following command:
$ sort -m file01 file02 file03
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.