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.
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 vwlsCharacter 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 UPPERCASETranslate Character Sets
Map one set of characters to another using the -t option: echo "12345" | tr -t '123' 'abc' Output:
abc45Deduplication 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:
RemoveextraspacesEscaping Special Characters
Escape Newlines
Replace newline characters with commas: echo "Line1\nLine2" | tr '\n' ',' Output:
Line1,Line2Escape Tabs
Replace tab characters with a hyphen: echo "Tab1\tTab2" | tr '\t' '-' Output:
Tab1-Tab2Character 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:
xxxdefMulti‑Character Replacement
Map multiple source characters to multiple target characters: echo "apple" | tr 'ae' '123' Output:
1ppl2Reading 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
