Combining and Sorting Output with sort, uniq, and cut
Learn to reshape and summarize streamed text by sorting lines, removing duplicates, and extracting fields with sort, uniq, and cut.
Combining and Sorting Output with sort, uniq, and cut is a free Linux Command Line Mastery lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Command Line Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why sort, uniq, and cut Matter
Filtering with grep and editing with sed/awk gets you far, but real pipelines often need to reorder, deduplicate, and slice columns of text.
sortorders linesuniqcollapses duplicatescutextracts fields
Together they turn raw streams into clean summaries.
Basic Sorting
sort reads lines and prints them in lexical order by default. It works great inside a pipe.
printf 'banana\napple\ncherry\n' | sortNumeric and Reverse Sort
Use -n for numeric order and -r to reverse. Without -n, 10 sorts before 2.
printf '10\n2\n33\n4\n' | sort -n -rSorting by a Specific Field
-k picks the column to sort on and -t sets the delimiter. Here we sort a CSV by the second field numerically.
printf 'alice,30\nbob,25\ncarol,40\n' | sort -t, -k2 -nRemoving Duplicates with uniq
uniq only collapses adjacent duplicate lines, so you almost always sort first.
printf 'a\na\nb\na\n' | sort | uniqCounting Occurrences
uniq -c prefixes each line with how many times it appeared. This is the classic frequency-count idiom.
printf 'err\nok\nerr\nerr\nok\n' | sort | uniq -cTop-N Frequency Report
Combine the tools to build a leaderboard: count, then sort the counts descending.
printf 'GET\nPOST\nGET\nGET\nPOST\n' | sort | uniq -c | sort -nrExtracting Columns with cut
cut -d sets the delimiter and -f selects fields. Here we grab the username column from a colon-separated record.
printf 'root:x:0\nalice:x:1000\n' | cut -d: -f1Cutting by Character Position
cut -c selects character ranges instead of fields, useful for fixed-width data.
printf 'ABCDEF\nGHIJKL\n' | cut -c2-4Finding Only Unique or Only Duplicated Lines
uniq -u prints lines that appear exactly once; uniq -d prints only those that repeat.
printf 'x\ny\nx\nz\n' | sort | uniq -dA Full Pipeline
Put it all together: extract a field, sort it, count duplicates, and rank. This pattern answers questions like which IP hit us most?
printf '1.1.1.1 GET\n2.2.2.2 GET\n1.1.1.1 POST\n' | cut -d' ' -f1 | sort | uniq -c | sort -nrQuick Check
Why must you usually run sort before uniq?
Recap
You can now reshape text streams end to end:
sort(with-n,-r,-k,-t) orders linesuniq(with-c,-d,-u) deduplicates and countscut(with-d/-for-c) extracts fields
The cut | sort | uniq -c | sort -nr chain is one of the most useful one-liners in Linux.
Frequently asked questions
Is the “Combining and Sorting Output with sort, uniq, and cut” lesson free?
Yes — the full text of “Combining and Sorting Output with sort, uniq, and cut” is free to read here on the web, and the Linux Command Line Mastery course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Command Line Mastery course, upgrade to CoddyKit PRO.
What will I learn in “Combining and Sorting Output with sort, uniq, and cut”?
Learn to reshape and summarize streamed text by sorting lines, removing duplicates, and extracting fields with sort, uniq, and cut. You practise Linux Command Line Mastery with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Linux Command Line Mastery?
No prior experience is required. Linux Command Line Mastery on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Combining and Sorting Output with sort, uniq, and cut” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Linux Command Line Mastery lesson?
Yes. Every Linux Command Line Mastery lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Standard Streams and Redirection
- Filtering Text with `grep`
- Text Manipulation with `sed` and `awk`
- Combining and Sorting Output with sort, uniq, and cut