LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

tr

Translate or delete characters

TLDR

Translate characters (lowercase to uppercase)
$ echo "hello" | tr 'a-z' 'A-Z'
copy
Delete specific characters
$ echo "hello 123" | tr -d '0-9'
copy
Squeeze repeated characters
$ echo "helllo" | tr -s 'l'
copy
Replace newlines with spaces
$ tr '\n' ' ' < [file]
copy
Delete non-printable characters
$ tr -cd '[:print:]' < [file]
copy
Translate spaces to tabs
$ tr ' ' '\t' < [file]
copy
Delete all except specified characters
$ echo "hello123" | tr -cd '0-9'
copy

SYNOPSIS

tr [options] set1 [set2]

DESCRIPTION

tr translates, deletes, or squeezes characters from standard input, writing to standard output. It operates character-by-character, making it efficient for simple transformations.Translation replaces each character in set1 with the corresponding character in set2. If set2 is shorter, its last character is repeated. If longer, excess characters are ignored.With -d, characters in set1 are deleted. With -s, repeated characters in set1 are squeezed to single occurrences. Both can be combined.-c complements set1, meaning "all characters NOT in set1." Useful for keeping only specific characters.

PARAMETERS

-d, --delete

Delete characters in set1
-s, --squeeze-repeats
Replace repeated characters with single occurrence
-c, -C, --complement
Use complement of set1
-t, --truncate-set1
Truncate set1 to length of set2

CHARACTER CLASSES

[:alnum:]: Letters and digits[:alpha:]: Letters[:digit:]: Digits[:lower:]: Lowercase letters[:upper:]: Uppercase letters[:space:]: Whitespace[:blank:]: Spaces and tabs[:print:]: Printable characters[:punct:]: Punctuation[:cntrl:]: Control characters

INSTALL

sudo apt install coreutils
copy
sudo dnf install coreutils
copy
sudo pacman -S coreutils
copy
sudo apk add coreutils
copy
sudo zypper install coreutils
copy
brew install coreutils
copy
nix profile install nixpkgs#coreutils
copy

CAVEATS

Tr operates on characters, not strings or patterns. For pattern-based substitution, use sed or awk.Character classes in brackets must use the [:class:] syntax. Don't confuse with regex [a-z] which also works but differs from [:lower:] in locales.Tr reads only from stdin; it cannot process files directly. Use redirection or pipes.

SEE ALSO

sed(1), awk(1), cut(1), iconv(1)

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid