使用 sort、uniq 和 cut 合并并排序输出
学习通过排序行、删除重复项以及使用 sort、uniq 和 cut 提取字段,重塑并汇总流式文本。
使用 sort、uniq 和 cut 合并并排序输出 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「使用 sort、uniq 和 cut 合并并排序输出」课时是免费的吗?
是的 — 「使用 sort、uniq 和 cut 合并并排序输出」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「使用 sort、uniq 和 cut 合并并排序输出」这节课中我会学到什么?
学习通过排序行、删除重复项以及使用 sort、uniq 和 cut 提取字段,重塑并汇总流式文本。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 sort、uniq 和 cut 合并并排序输出」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 标准流与重定向
- 使用 `grep` 过滤文本
- 使用 `sed` 和 `awk` 操作文本
- 使用 sort、uniq 和 cut 合并并排序输出