Transforming text

This topic covers the commands that reshape text: putting lines in order, dropping duplicates, swapping characters, reflowing paragraphs to a width, counting what is there. Its companion, Viewing and joining text, covers printing and combining files; this topic is about changing the text itself.

The commands #

Ordering lines

CommandPurpose
sortSort lines — alphabetically, numerically, and many other ways.
shufThe opposite of sorting: put lines into a random order.
tsortTopological sort — order items so that each comes after the things it depends on.

Filtering lines

CommandPurpose
uniqCollapse or report adjacent repeated lines.

Substituting characters

CommandPurpose
trTranslate, squeeze, or delete individual characters.

Whitespace

CommandPurpose
expandConvert tabs into spaces.
unexpandConvert runs of spaces back into tabs.

Reflowing and paginating

CommandPurpose
fmtReflow paragraphs to a target width.
foldHard-wrap long lines at a fixed width.
prPaginate and columnate text for printing.

Indexing and counting

CommandPurpose
ptxProduce a permuted index of the words in a file.
wcCount the lines, words, and bytes in a file.

Two commands that need sorted input #

A theme worth knowing before you start: uniq only collapses adjacent duplicate lines, so duplicates scattered through a file are not caught unless the file is sorted first. sort and uniq are almost always used together — and sort -u does both jobs in one step.

Where to start #

sort is the workhorse of the topic and the one with the most depth — start there.

Edit this page