Linux tr Command: Translating and Squeezing
The tr (translate) command is a core utility in the
Linux operating system designed for stream-based character manipulation.
This article explores the significance of tr in Linux text
processing, detailing how it functions within pipelines to translate
characters, squeeze repeated sequences, and delete unwanted elements,
along with practical examples demonstrating why it remains a
foundational tool for system administrators and developers.
What is the tr
Command?
The tr utility reads from standard input
(stdin), transforms or filters the input stream based on
specified character sets, and writes the result to standard output
(stdout). Unlike tools like sed or
awk, tr does not work directly with filenames
as arguments; it operates exclusively on streams, making it a critical
component of command-line pipelines.
Character Translation
The primary function of tr is mapping characters from a
source set to a destination set on a one-to-one basis. This is crucial
for normalization tasks, such as standardizing file content or
environment variables.
- Case Conversion: It provides a lightweight way to
change case without regular expressions.
cat file.txt | tr 'a-z' 'A-Z' - Character Swapping: Individual characters can be
replaced globally throughout a stream.
cat data.csv | tr ',' '\t'
Squeezing Repeated Characters
(-s)
The squeezing functionality (-s or
--squeeze-repeats) replaces consecutive duplicate instances
of a specified character with a single instance. This capability is
significant for data cleaning and formatting raw system outputs.
- Normalizing Whitespace: System commands like
ps,df, ornetstatoutput columns separated by variable numbers of spaces. Squeezing reduces these to single delimiters, making the data easier to parse with tools likecut.cat log.txt | tr -s ' ' - Removing Redundant Newlines: Empty blank lines
caused by multiple consecutive newline characters can be condensed into
a single line break.
cat text.txt | tr -s '\n'
Deleting Characters
(-d)
The deletion flag (-d) removes specific characters from
the input stream entirely. This is widely used for:
- Removing Carriage Returns: Converting
Windows-formatted line endings (CRLF) to Unix line endings (LF).
cat script.sh | tr -d '\r' - Stripping Non-Alphanumeric Data: Isolating pure
data by deleting symbols, punctuation, or digits.
cat input.txt | tr -d '0-9'
Why tr is
Significant in Linux
- Performance and Efficiency: Written in optimized C,
trexecutes substantially faster than larger text-processing utilities likesed,awk, orperlwhen performing byte-level and character-level operations. - Stream Integration: Because it operates strictly on
standard input,
trintegrates natively into Unix pipelines (|), allowing modular data transformations without generating temporary intermediate files. - Simplicity in Automation: The syntax is concise, making shell scripts easier to write, read, and maintain when performing everyday tasks such as sanitizing file names, parsing logs, or reformatting structured text.