📖 Scenario: You work in a small office where you receive a list of names with duplicates. You want to clean this list by sorting the names alphabetically and removing duplicates.
🎯 Goal: Build a bash script pipeline that sorts a list of names and removes duplicates using sort and uniq.
📋 What You'll Learn
Create a file named names.txt with a list of names including duplicates
Use a variable to hold the filename
Use a pipeline with sort and uniq to process the file
Print the final sorted and unique list of names
💡 Why This Matters
🌍 Real World
Cleaning and organizing lists of data like names, emails, or product IDs is common in office work and scripting tasks.
💼 Career
Knowing how to use pipelines with <code>sort</code> and <code>uniq</code> is a basic but essential skill for system administrators, data analysts, and anyone working with text data on the command line.
Progress0 / 4 steps
1
Create the names file
Create a file called names.txt with these exact names, each on its own line: Alice, Bob, Charlie, Alice, Bob, David
Bash Scripting
Hint
Use echo -e with newline characters \n and redirect output to names.txt.
2
Set the filename variable
Create a variable called filename and set it to the string names.txt
Bash Scripting
Hint
Use filename="names.txt" to assign the variable.
3
Sort and remove duplicates using a pipeline
Use a pipeline with sort and uniq commands to read from the file stored in filename and produce a sorted list without duplicates. Store the result in a variable called clean_names
Bash Scripting
Hint
Use command substitution with $( ) to save the pipeline output to clean_names.
4
Print the cleaned list
Print the variable clean_names to display the sorted list of unique names
Bash Scripting
Hint
Use echo "$clean_names" to print the variable preserving line breaks.
Practice
(1/5)
1. What is the main purpose of using sort | uniq together in a bash pipeline?
easy
A. To get a sorted list with duplicates removed
B. To count the number of lines in a file
C. To sort lines but keep duplicates
D. To find the longest line in a file
Solution
Step 1: Understand what sort does
The sort command arranges lines in alphabetical or numerical order.
Step 2: Understand what uniq does after sorting
The uniq command removes duplicate adjacent lines, so sorting first groups duplicates together for removal.
Final Answer:
To get a sorted list with duplicates removed -> Option A
Quick Check:
sort + uniq = sorted unique lines [OK]
Hint: Always sort before uniq to remove all duplicates [OK]
Common Mistakes:
Using uniq before sort, which misses duplicates
Thinking uniq counts lines instead of removing duplicates
Assuming sort removes duplicates by itself
2. Which of the following is the correct syntax to remove duplicate lines from file.txt and sort the output?
easy
A. uniq file.txt | sort
B. sort file.txt | uniq
C. sort | uniq file.txt
D. uniq | sort file.txt
Solution
Step 1: Identify correct command order
The file must be sorted first, so sort file.txt comes before uniq.
Step 2: Check syntax correctness
sort file.txt | uniq correctly pipes sorted output to uniq for duplicate removal.
Hint: Sort first to group duplicates, then uniq removes repeats [OK]
Common Mistakes:
Not sorting first, so uniq misses duplicates
Expecting original order instead of sorted
Confusing uniq output with counting duplicates
4. You run uniq file.txt | sort to remove duplicates and sort lines, but duplicates remain. What is the likely problem?
medium
A. uniq only removes adjacent duplicates, so sorting after uniq misses duplicates
B. sort command is not working without options
C. file.txt is empty, so no output
D. uniq requires -c option to remove duplicates
Solution
Step 1: Understand uniq behavior
Uniq removes only adjacent duplicate lines, so if duplicates are scattered, it won't remove them.
Step 2: Analyze command order effect
Sorting after uniq means duplicates are not grouped before uniq runs, so duplicates remain.
Final Answer:
uniq only removes adjacent duplicates, so sorting after uniq misses duplicates -> Option A
Quick Check:
uniq needs sorted input to remove all duplicates [OK]
Hint: Sort before uniq to group duplicates for removal [OK]
Common Mistakes:
Thinking uniq removes all duplicates regardless of order
Believing sort alone removes duplicates
Using uniq -c instead of uniq for removal
5. You have a log file access.log with many repeated IP addresses scattered randomly. You want a sorted list of unique IPs. Which command pipeline correctly achieves this?
hard
A. cat access.log | uniq | sort
B. uniq access.log | sort
C. sort -u access.log
D. uniq access.log | sort -u
Solution
Step 1: Understand sort -u option
sort -u sorts and removes duplicates in one step efficiently.
Step 2: Why others fail
Options A, B, and D all apply uniq before sorting, missing non-adjacent duplicates since IPs are scattered randomly.
Final Answer:
sort -u access.log -> Option C
Quick Check:
sort -u = sorted unique lines in one command [OK]
Hint: Use sort -u for sorted unique lines in one step [OK]
Common Mistakes:
Using uniq before sort, missing duplicates
Not knowing sort -u combines sorting and uniqueness
Using unnecessary cat command
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.