Sorting and columns · 排序与列
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Putting lines in order
sortarranges lines alphabetically (or, with-n, as numbers):
sort -rreverses the order;sort -nis for lists of numbers.
中文
把各行排好序
sort把各行按字母顺序排列(加上-n则按数字排列):
sort names.txt
sort -r反转顺序;sort -n用于数字列表。
English
Removing duplicates
uniqremoves neighbouring repeated lines. Because it only looks at neighbours, you usuallysortfirst:
- Add
uniq -cto also count how many times each line appeared.
中文
去除重复
uniq会去掉相邻的重复行。因为它只看相邻的行,所以你通常要先sort:
sort names.txt | uniq
- 加上
uniq -c,还能统计每一行出现了多少次。
Explore · 探索
sort, then uniq · 先 sort,再 uniq
uniq only sees neighbours — sort first, so the duplicates sit together. · uniq 只看相邻行 —— 先 sort,重复的行才会挨在一起。
English
Picking out columns
- Many files have columns separated by a character — a comma, a colon, a tab.
cutpulls out the columns you want.-dsets the delimiter,-fthe field:
- That takes the first field of each
name:ageline — the names.
中文
取出某一列
- 很多文件的各列之间用某个字符分隔 —— 逗号、冒号或制表符。
cut取出你想要的列。-d设置分隔符,-f设置字段:
cut -d : -f 1 people.txt
- 这会取每行
name:age的第一个字段 —— 也就是名字。
English
Common mistakes
sortis alphabetical;sort -nsorts numbers correctly.cutneeds the right delimiter with-d.
中文
常见错误
sort按字母排序;sort -n才能正确排数字。cut要用-d指定正确的分隔符。
Get a tidy, de-duplicated list of names. Pipe sort into · 生成 uniq: sort names.txt | uniq. · 得到一份整洁、去重后的名字列表。把 sort 接到 uniq:sort names.txt | uniq。
people.txt has name:age on each line. Pull out just the names with cut -d : -f 1 people.txt. · people.txt 每行是 name:age。用 cut -d : -f 1 people.txt 只取出名字这一列。