tr — Character Translation
Character-level translation, deletion, and squeeze. Operates on characters, not strings or fields — the preprocessing step before sort, awk, or grep.
Character Translation
tr translates, deletes, or squeezes characters. It operates on characters, not strings or fields.
Case conversion
echo "HELLO-WORLD" | tr '[:upper:]' '[:lower:]'
# Output: hello-world
echo "quiet" | tr '[:lower:]' '[:upper:]'
# Output: QUIET
Character substitution — dashes to underscores
echo "port-smtp" | tr '-' '_'
# Output: port_smtp
# Used in antora.yml attribute-to-env-var conversion:
echo "mail-hostname" | tr '-' '_' | tr '[:lower:]' '[:upper:]'
# Output: MAIL_HOSTNAME
Delete characters (-d)
echo "{search_id}" | tr -d '{}'
# Output: search_id
echo " spaces " | tr -d ' '
# Output: spaces
Squeeze repeated characters (-s)
echo "too many spaces" | tr -s ' '
# Output: too many spaces
# Squeeze newlines — collapse blank lines
cat file.txt | tr -s '\n'
Replace non-printable characters
# Replace all non-alphanumeric with underscore
echo "file (v2.0) final!.txt" | tr -cs '[:alnum:]._' '_'
# Output: file_v2.0_final_.txt
# -c = complement (everything NOT in the set)
# -s = squeeze repeated replacements
Translate for pipeline use — strip braces from attribute references
grep -oP '\{\w+\}' file.adoc | tr -d '{}' | sort -u
# Output: clean attribute names without braces
See Also
-
sed — string-level transforms (tr works on characters, sed on patterns)
-
cut — field-level extraction
-
Parameter Expansion — case conversion in pure bash