250+ TOP MCQs on Unique and tr Command and Answers

Unix Multiple Choice Questions on “Unique and tr Command”.

1. Which command is used for locating repeated and non-repeated lines?
a) sort
b) uniq
c) cut
d) paste

Answer: b
Clarification: When we concatenate or merge files, we can encounter the problem of duplicate entries creeping in. UNIX offers a special command (uniq) which can be used to handle these duplicate entries. We can also use the sort -u command for doing the same piece of work.

2. uniq command requires a sorted file as input.
a) True
b) False

Answer: a
Clarification: uniq command is used for locating repeated and non-repeated entries. But the most important requirement for using uniq command is, the file should be sorted. For example,

$ sort dept.lst |  uniq  -  uniqlist        // output in uniqlist

3. Which option is used with uniq command for selecting non-repeated lines?
a) -i
b) -c
c) -u
d) -a

Answer: c
Clarification: To select unique lines, we can use the sort -u command. But uniq also offers an option (-u) which is used for selecting non-repeating entries in a field. For example,

$ cut  -d  “|” -f3 emp.lst | sort  |uniq -u   
 // cut 3rd field from emp.lst, sort it and find unique entries

4. Which option is used for selecting repeated entries?
a) -d
b) -c
c) -u
d) -a

Answer: a
Clarification: The -d option when used with uniq command lets us select only one copy of duplicate entries. For example,

$ cut  -d  “|” -f3 emp.lst | sort  |uniq -d
d.g.m                    // only single copy of all duplicate entries
director
executive
g.m. 
manager

5. ______ option is used for counting frequency of occurrence.
a) -d
b) -c
c) -u
d) -a

Answer: b
Clarification: We can use the -c option with uniq command to count the frequency of occurrence of all lines along with the lines. For example,

$ cut  -d  “|” -f3 emp.lst | sort  |uniq -c
1 d.g.m                    
2 director
4 executive
4 g.m. 
2 manager

6. The output of the following command will be:

a) erroneous
b) output stored in foo2
c) concatenates both files
d) process foo1 and output is stored in foo2

Answer: d
Clarification: Like sort command, uniq command also accepts the output filename as an argument, but without using -o option. If we use the above command, it will simply process fo1 and overwrites foo2 with its output.

7. Which command is used for translating characters?
a) sort
b) trans
c) tr
d) paste

Answer: c
Clarification: The translate (tr) filter is used for manipulating individual characters in a line. Usually, tr command translates characters using one or two compact expressions. The syntax for using tr command is,

$ tr  options  expression1  expression2  standard input

8. To replace | with ~, which one of the following commands will be used?
a) tr ‘|’ ‘~-‘
b) tr ‘|’ ‘~-‘ > emp.lst
c) tr | ~ emp.lst
d) !

Answer: b
Clarification: The translate (tr) filter is used for manipulating individual characters in a line. It replaces the first character in the first expression with the first character in the second expression.

9.

head -n 3 emp.lst | tr ‘[a-z]’  ‘[A-Z]’

Above command will change the case of text from lower to upper.
a) True
b) False

Answer: a
Clarification: Since we know that tr command doesn’t accept the filename as an argument, the input has to be redirected from a file or a pipe. Above command will change the case of first three lines of emp.lst from lowercase to uppercase.

10. Which option is used with tr command for deleting characters?
a) -d
b) -c
c) -n
d) -a

Answer: a
Clarification: By using tr command we can delete the characters from a file. To perform this operation, we’ve to use -d option. For example,

$ tr  -d  ‘|/’ < emp.lst |head  -n 3        //deletes | and / from the first three lines

11. ______ option is used for compressing multiple consecutive characters.
a) -d
b) -c
c) -n
d) -s

Answer: d
Clarification: tr command can also remove multiple consecutive characters from a line by using -s option. This will make the line squeeze from multiple consecutive occurrences of a character to a single character. For example, to squeeze multiple contiguous spaces use the following command:

$ tr  -s  ‘  ‘ < emp.lst | head -n  3

Leave a Reply

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