0
0
Bash Scriptingscripting~5 mins

sort and uniq in pipelines in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ABecause <code>uniq</code> sorts the lines automatically
BBecause <code>sort</code> removes duplicates
CBecause <code>uniq</code> only removes adjacent duplicate lines
DBecause <code>sort</code> counts duplicates
What does the command sort file.txt | uniq -c do?
ASorts lines and counts how many times each unique line appears
BSorts lines and removes all duplicates without counting
CCounts lines without sorting
DRemoves duplicates but does not sort
What happens if you run uniq file.txt without sorting first?
AOnly adjacent duplicate lines are removed
BAll duplicates in the file are removed
CThe file is sorted automatically
DThe command will fail
Which command pipeline shows unique lines sorted alphabetically?
A<code>sort file.txt | uniq -c</code>
B<code>cat file.txt | uniq | sort</code>
C<code>uniq file.txt | sort</code>
D<code>cat file.txt | sort | uniq</code>
What does the -c option do with uniq?
ARemoves all lines
BCounts occurrences of each unique line
CSorts the lines
DChanges the case of lines
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.