The sort command arranges lines of text in a file or input alphabetically or numerically. It helps organize data so it's easier to read or process.
Click to reveal answer
beginner
What is the purpose of the uniq command?
uniq filters out repeated lines that are next to each other in a file or input. It shows only one copy of each repeated line.
Click to reveal answer
beginner
Why do you often use sort before uniq?
Because uniq only removes duplicates that are next to each other, sorting first groups duplicates together so uniq can remove all repeated lines.
Click to reveal answer
intermediate
How do you count how many times each line appears using uniq?
Use uniq -c. It adds a number before each line showing how many times that line appeared consecutively.
Click to reveal answer
beginner
What does the command sort file.txt | uniq do?
It sorts the lines in file.txt alphabetically, then removes any duplicate lines, showing only unique lines in order.
Click to reveal answer
What happens if you run uniq file.txt without sorting first?
AOnly consecutive duplicate lines are removed
BAll duplicate lines are removed regardless of position
CThe file is sorted alphabetically
DNothing happens
✗ Incorrect
uniq only removes duplicates that are next to each other. Without sorting, duplicates separated by other lines stay.
Which command shows how many times each line appears in a sorted file?
Auniq -c file.txt
Bsort file.txt | uniq -c
Csort -c file.txt
Duniq file.txt
✗ Incorrect
Sorting first groups duplicates, then uniq -c counts each group.
What does sort -r do?
ASorts lines in reverse order
BRemoves duplicates
CCounts lines
DSorts lines randomly
✗ Incorrect
-r option sorts lines in reverse (descending) order.
How can you remove duplicate lines ignoring case differences?
Asort | uniq
Bsort -n
Cuniq -c
Dsort -f | uniq -i
✗ Incorrect
sort -f ignores case, and uniq -i ignores case when filtering duplicates.
What is the output of echo -e "apple\nApple\napple" | sort | uniq?
Aapple
Bapple
Apple
apple
CApple
apple
DApple
✗ Incorrect
Sorting puts 'Apple' before 'apple'. uniq sees them as different because of case, so both appear.
Explain how to use sort and uniq together to find unique lines in a file.
Think about why sorting helps uniq work better.
You got /3 concepts.
Describe how to count the number of times each unique line appears in a text file using Linux commands.
Counting needs grouping first.
You got /3 concepts.
Practice
(1/5)
1. What does the uniq command do when used alone on a text file?
easy
A. Deletes all empty lines
B. Removes repeated adjacent lines
C. Counts all lines in the file
D. Sorts all lines alphabetically
Solution
Step 1: Understand the uniq command behavior
The uniq command filters out repeated lines only if they are next to each other.
Step 2: Compare with other options
It does not sort lines or count all lines; it only removes adjacent duplicates.
Final Answer:
Removes repeated adjacent lines -> Option B
Quick Check:
uniq removes adjacent duplicates = D [OK]
Hint: uniq only removes duplicates if they are next to each other [OK]
Common Mistakes:
Thinking uniq sorts lines
Assuming uniq removes all duplicates anywhere
Confusing uniq with wc (word count)
2. Which of the following is the correct syntax using a pipe to sort a file named data.txt and remove duplicate lines?
easy
A. sort data.txt | uniq
B. uniq data.txt | sort
C. sort -u data.txt
D. uniq -s data.txt
Solution
Step 1: Understand the order of commands
To remove duplicates correctly, lines must be sorted first so duplicates become adjacent.
Step 2: Check each option
sort data.txt | uniq sorts then pipes to uniq, which removes adjacent duplicates. uniq data.txt | sort runs uniq first, which won't remove all duplicates if unsorted. sort -u data.txt uses sort -u which is valid but not the pipe syntax asked. uniq -s data.txt is invalid syntax.
Final Answer:
sort data.txt | uniq -> Option A
Quick Check:
Sort before uniq to remove duplicates = A [OK]
Hint: Always sort before uniq to remove duplicates correctly [OK]
uniq removes repeated adjacent lines, so only one of each remains: alice, bob, carol.
Final Answer:
alice
bob
carol -> Option A
Quick Check:
Sort then uniq removes duplicates = alice, bob, carol [OK]
Hint: Sort first, then uniq removes duplicates in order [OK]
Common Mistakes:
Not sorting before uniq
Expecting uniq to remove all duplicates anywhere
Confusing output order
4. You run sort data.txt | uniq but notice duplicates remain. What is the most likely cause?
medium
A. sort command does not work on text files
B. uniq command is misspelled
C. The file contains lines with trailing spaces or tabs
D. The file is empty
Solution
Step 1: Understand how sort and uniq treat whitespace
sort and uniq treat lines with trailing spaces or tabs as different lines, so duplicates with extra spaces won't be adjacent identical.
Step 2: Check other options
uniq misspelling would cause error, not duplicates remaining. sort works on text files. Empty file means no duplicates.
Final Answer:
The file contains lines with trailing spaces or tabs -> Option C
Quick Check:
Trailing spaces cause uniq to miss duplicates = C [OK]
Hint: Check for trailing spaces if duplicates remain after sort|uniq [OK]
Common Mistakes:
Assuming uniq always removes duplicates regardless of spaces
Ignoring trailing whitespace differences
Thinking sort fails on text files
5. You want to count how many unique IP addresses appear in access.log. Which command correctly sorts, removes duplicates, and counts unique lines?
hard
A. sort access.log | uniq -c
B. uniq access.log | wc -l
C. uniq -c access.log | sort
D. sort -u access.log | wc -l
Solution
Step 1: Understand the goal
You want the count of unique IP addresses, so you need to remove duplicates and count the unique lines.
Step 2: Analyze each option
sort access.log | uniq -c counts occurrences but does not sum total unique lines. uniq -c access.log | sort counts duplicates before sorting, so misses some. sort -u access.log | wc -l uses sort -u to get unique lines, then counts lines with wc -l, which gives the unique count. uniq access.log | wc -l counts lines after uniq without sorting, so duplicates may remain.
Final Answer:
sort -u access.log | wc -l -> Option D
Quick Check:
sort -u then wc -l counts unique lines = B [OK]
Hint: Use sort -u then wc -l to count unique lines fast [OK]
Common Mistakes:
Using uniq -c without sorting first
Counting lines before removing duplicates
Confusing uniq -c output with total unique count
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.