tr translate or delete characters

tr works on single characters rather than words: it maps one set of characters to another, deletes characters, or squeezes repeats. It reads only from standard input, so use it in a pipe or with <.

Availability Preinstalled on macOS, Linux and BSD

Syntax

tr [options] set1 [set2]

Common options

OptionWhat it does
'a-z' 'A-Z'Map each character in the first set to the second
-d setDelete these characters
-s setSqueeze repeated characters into one

Examples

Upper-case text

echo hello | tr 'a-z' 'A-Z'

Remove Windows carriage returns

tr -d '\r' < windows.txt > unix.txt

Collapse repeated spaces

tr -s ' ' < report.txt

Use tr with sqx

Turn a comma-separated list into rows

Each item becomes a line, and sqx -i lines makes each line a row.

echo "example.com,go.dev,iana.org" | tr ',' '\n' | sqx -i lines "SELECT line AS domain, dns_ip(line) AS ip FROM stdin"

Full documentation

This page covers the essentials. For every option, see the official documentation:

Or read the manual in your terminal: man tr