Recall & Review
beginner
What does the
sort command do in a pipeline?The
sort command arranges lines of text in order, usually alphabetically or numerically, making it easier to find duplicates or organize data.Click to reveal answer
beginner
What is the purpose of the
uniq command in a pipeline?The
uniq command filters out repeated adjacent lines, showing only unique lines. It works best when input is sorted first.Click to reveal answer
beginner
Why do we usually use
sort before uniq in pipelines?Because
uniq only removes duplicates that are next to each other, sorting first groups duplicates together so uniq can remove them properly.Click to reveal answer
intermediate
How can you count how many times each unique line appears using
sort and uniq?Use
sort | uniq -c. The -c option tells uniq to prefix each unique line with the number of times it appears.Click to reveal answer
beginner
What will the command
cat file.txt | sort | uniq do?It will read the file
file.txt, sort all lines alphabetically, then remove any duplicate lines, showing only unique sorted lines.Click to reveal answer
Why should you use
sort before uniq in a pipeline?✗ Incorrect
uniq only removes duplicates if they are next to each other. Sorting first groups duplicates together so uniq can remove them.
What does the command
sort file.txt | uniq -c do?✗ Incorrect
The -c option with uniq counts occurrences of each unique line after sorting.
What happens if you run
uniq file.txt without sorting first?✗ Incorrect
uniq only removes duplicates that are next to each other, so unsorted duplicates remain.
Which command pipeline shows unique lines sorted alphabetically?
✗ Incorrect
Sorting first then applying uniq ensures duplicates are removed and lines are sorted.
What does the
-c option do with uniq?✗ Incorrect
The -c option prefixes each unique line with the count of how many times it appears.
Explain why sorting is important before using
uniq in a pipeline.Think about how <code>uniq</code> works with lines next to each other.
You got /3 concepts.
Describe how to count the number of times each unique line appears in a text file using bash commands.
Combine sort and uniq with a special option.
You got /3 concepts.