0
0
Bash Scriptingscripting~15 mins

sort and uniq in pipelines in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using sort and uniq in pipelines
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo "$clean_names" to print the variable preserving line breaks.