Unix Questions and Answers – Unique and tr Command

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

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

advertisement
advertisement
$ 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
View Answer

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ 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
View Answer

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

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

advertisement

6. The output of the following command will be:

$ uniq  foo1 foo2

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

Answer: d
Explanation: 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
View Answer

Answer: c
Explanation: 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) !
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

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

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.