Sorting and columns
Putting lines in order
sortarranges lines alphabetically (or, with-n, as numbers):
sort names.txt
sort -rreverses the order;sort -nis for lists of numbers.
Removing duplicates
uniqremoves neighbouring repeated lines. Because it only looks at neighbours, you usuallysortfirst:
sort names.txt | uniq
- Add
uniq -cto also count how many times each line appeared.

sort, then uniq
uniq only sees neighbours — sort first, so the duplicates sit together.
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:
cut -d : -f 1 people.txt
- That takes the first field of each
name:ageline — the names.
Common mistakes
sortis alphabetical;sort -nsorts numbers correctly.cutneeds the right delimiter with-d.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Get a tidy, de-duplicated list of names. Pipe sort thành uniq: sort names.txt | uniq.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
people.txt has name:age on each line. Pull out just the names with cut -d : -f 1 people.txt.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
How many times does each name appear in names.txt? Show each name once, with its số lượng in front.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
sizes.txt holds three file sizes. Sort them as numbers, largest first. (Plain sort -r puts 9 first: it compares text.)