Fundamentals 6 min read

Master the Linux ‘tr’ Command: Practical Text‑Manipulation Examples

This guide explains how to use the powerful Linux tr command for character replacement, deletion, case conversion, whitespace compression, special‑character escaping, and reading character sets from files, providing clear examples and step‑by‑step code snippets for each use case.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master the Linux ‘tr’ Command: Practical Text‑Manipulation Examples

Basic Character Replacement

The most common use of tr is to replace characters. For example, replacing all lowercase "o" with the digit "0": echo "Hello, World!" | tr 'o' '0' Output:

Hell0, W0rld!

Delete Characters

Use the -d option to delete specific characters, such as all vowels: echo "Remove all vowels" | tr -d 'aeiou' Output:

Rmv ll vwls

Character Set Conversion

Case Conversion

Convert uppercase to lowercase or vice‑versa:

echo "Convert To Lowercase" | tr 'A-Z' 'a-z'
echo "Convert To Uppercase" | tr 'a-z' 'A-Z'

Outputs: convert to lowercase and

CONVERT TO UPPERCASE

Translate Character Sets

Map one set of characters to another using the -t option: echo "12345" | tr -t '123' 'abc' Output:

abc45

Deduplication and Whitespace Compression

Remove Duplicate Characters

Compress consecutive spaces into a single space with -s: echo "Hello, World!" | tr -s ' ' Output:

Hello, World!

Delete All Whitespace

Delete every space character using -d: echo "Remove extra spaces" | tr -d ' ' Output:

Removeextraspaces

Escaping Special Characters

Escape Newlines

Replace newline characters with commas: echo "Line1\nLine2" | tr '\n' ',' Output:

Line1,Line2

Escape Tabs

Replace tab characters with a hyphen: echo "Tab1\tTab2" | tr '\t' '-' Output:

Tab1-Tab2

Character Ranges and Multi‑Character Replacement

Range Replacement

Replace a range of characters, e.g., ‘a‑c’ with ‘x’: echo "abcdef" | tr 'a-c' 'x' Output:

xxxdef

Multi‑Character Replacement

Map multiple source characters to multiple target characters: echo "apple" | tr 'ae' '123' Output:

1ppl2

Reading Character Sets from Files

Use -s together with -f to read a character set from a file, useful for large sets: echo "abcde" | tr -s -d -f char_set.txt Here char_set.txt contains the characters to delete, and -f tells tr to read the set from that file.

Summary

The Linux tr utility is a versatile tool for text processing, capable of character replacement, deletion, case conversion, whitespace handling, special‑character escaping, and reading sets from files, making it essential for scripting and data transformation tasks.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcommand-linetext processingShell scriptingUnix utilitiestr command
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.