Sort and count

Introduction

Coreutils filter and reshape text streams: sort lines, count duplicates, extract fields, translate characters, and save output while a pipeline keeps running.

Frequency count

Sample lines

$ cat colors.txt
red
blue
red
green
blue
red

Count

$ sort colors.txt | uniq -c | sort -rn
      3 red
      2 blue
      1 green

sort groups equal lines, uniq -c counts each run, and sort -rn orders by count, largest first.

See: uniq invocation

Sort lines

$ sort colors.txt
blue
blue
green
red
red
red

Options

FlagEffect
-rReverse order
-uKeep one of each line
-nCompare as numbers
-kSort by a key field

By default, sort compares whole lines using the current locale.

See: sort invocation

Unique lines

$ sort -u colors.txt
blue
green
red

-u sorts and removes duplicate lines in one pass.

See: sort invocation

Adjacent duplicates

$ printf 'red\nred\nblue\nred\n' | uniq
red
blue
red

uniq compares only neighbouring lines; sort first when duplicates can be apart.

See: uniq invocation

Repeated lines only

$ sort colors.txt | uniq -d
blue
red

-d prints one copy of each repeated line; -u prints lines that occur once.

See: uniq invocation

Sort by key

Numeric field

Sample rows

$ cat people.csv
ann,30,dev
bob,25,ops
cy,41,dev

Sort

$ sort -t, -k2,2n people.csv
bob,25,ops
ann,30,dev
cy,41,dev

-t, splits on commas; -k2,2n sorts field two as a number.

See: sort invocation

Text field

$ sort -t, -k3,3 people.csv
ann,30,dev
cy,41,dev
bob,25,ops

-k3,3 limits the key to field three; adding r reverses the order.

See: sort invocation

Multiple keys

$ sort -t, -k3,3 -k2,2nr people.csv
cy,41,dev
ann,30,dev
bob,25,ops

The first key groups by role; the second sorts each group by descending age.

See: sort invocation

Extract fields

By delimiter

$ cut -d, -f1 people.csv
ann
bob
cy

-d sets the delimiter; -f selects fields, starting at one.

See: cut invocation

By character position

$ cut -c1-3 colors.txt
red
blu
red
gre
blu
red

-c selects characters by position; -b selects bytes. -c1-8 is a range, -c5- runs to the end.

See: cut invocation

Field lists

$ cut -d, -f1,3 people.csv
ann,dev
bob,ops
cy,dev

-f1,3 picks two fields; -f2- keeps a field and everything after it.

See: cut invocation

Translate characters

Uppercase

$ echo 'Hello, World!' \
    | tr '[:lower:]' '[:upper:]'
HELLO, WORLD!

tr maps each character in the first set to the one at the same position in the second. Ranges like 'a-z' work too.

See: tr invocation

Delete characters

$ echo 'a-b-c' | tr -d '-'
abc

-d removes matching characters; use tr -d '\r' to strip carriage returns.

See: tr invocation

Squeeze repeats

$ echo 'too   many    spaces' | tr -s ' '
too many spaces

-s collapses each run of matching characters into one.

See: tr invocation

Split on a separator

$ echo 'a,b,c' | tr ',' '\n'
a
b
c

Mapping a delimiter to a newline turns a list into one item per line.

See: tr invocation

Save output with tee

Write and display

$ echo hello | tee out.txt
hello

tee writes stdin to a file and to stdout, so the pipeline keeps running.

See: tee invocation

Append

$ echo again | tee -a out.txt
again

-a appends instead of replacing the file.

See: tee invocation

Multiple files

$ echo shared | tee a.txt b.txt
shared

Several paths write the same stream to each file.

See: tee invocation

Combine tools

Column frequencies

$ cut -d, -f3 people.csv \
    | sort | uniq -c | sort -rn
      2 dev
      1 ops

cut picks the column, then sort | uniq -c counts its values.

See: cut invocation

Most frequent lines

$ sort colors.txt | uniq -c \
    | sort -rn | head -n 2
      3 red
      2 blue

head stops the pipeline after the ranked results you need.

See: sort invocation

Also see

0 Comments for this cheatsheet. Write yours!