Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Linux tr Command
The tr (translate) command is a Linux utility that allows you to perform various transformations on text input. Whether you need to change case, remove repeating characters, delete characters, set complements, or replace specific characters, tr can handle it all. In this article, we'll explore the various options available with the tr command and show you how to use them to transform text.
Syntax
The basic syntax of the tr command is as follows ?
tr [OPTIONS] SET1 [SET2]
Where ?
SET1 is a set of characters to be replaced by the corresponding characters in SET2.
SET2 is a set of characters that replaces the corresponding characters in SET1.
OPTIONS include flags like
-d(delete),-s(squeeze),-c(complement).
The tr command reads from standard input and writes to standard output. Characters in SET1 and SET2 can be specified as individual characters, ranges like [a-z], or character classes like [:upper:].
Basic example replacing one character with another ?
$ echo "Hello World" | tr 'H' 'Y'
Yello World
Change Character Case
The tr command can change the case of input text in several ways.
Using Character Ranges
Convert all uppercase letters to lowercase using ranges ?
$ echo "Hello World" | tr '[A-Z]' '[a-z]'
hello world
Using Character Classes
Use predefined character classes for case conversion ?
$ echo "HELLO WORLD" | tr '[:upper:]' '[:lower:]'
hello world
Similarly, convert lowercase to uppercase ?
$ echo "hello world" | tr '[:lower:]' '[:upper:]'
HELLO WORLD
Remove Repeated Characters
The -s (squeeze) option compresses repeated characters into a single instance. This is useful for cleaning up text with multiple consecutive spaces or other repeated characters.
$ echo "Hello World" | tr -s ' '
Hello World
Replace multiple spaces with a single tab ?
$ echo "Hello World" | tr -s ' ' '\t'
Hello World
Delete Characters
Use the -d option to delete specific characters from input.
Delete Specific Characters
$ echo "Hello World" | tr -d 'e'
Hllo World
Delete Character Classes
Remove all digits from text ?
$ echo "Hello 123 World" | tr -d '[:digit:]'
Hello World
Delete with Complement
Use the -c option to complement the character set, keeping only the specified characters ?
$ echo "Hello 123 World" | tr -cd '[:digit:]'
123
Common Use Cases
Convert Spaces to Newlines
Split words into separate lines ?
$ echo "Hello World Linux" | tr ' ' '<br>'
Hello World Linux
Remove Newlines
Convert multiple lines into a single line ?
$ printf "Hello\nWorld\nLinux" | tr '<br>' ' '
Hello World Linux
Character Translation Table
| Character Class | Description | Example |
|---|---|---|
| [:alnum:] | Alphanumeric characters | a-z, A-Z, 0-9 |
| [:alpha:] | Alphabetic characters | a-z, A-Z |
| [:digit:] | Digit characters | 0-9 |
| [:lower:] | Lowercase letters | a-z |
| [:upper:] | Uppercase letters | A-Z |
| [:space:] | Whitespace characters | Space, tab, newline |
| [:punct:] | Punctuation characters | !@#$%^&*() |
Advanced Options
| Option | Description | Example Usage |
|---|---|---|
| -c | Complement the set | tr -c '[:digit:]' ' ' |
| -d | Delete characters | tr -d 'aeiou' |
| -s | Squeeze repeated characters | tr -s ' ' |
| -t | Truncate SET1 to length of SET2 | tr -t 'abc' 'xy' |
Conclusion
The tr command is a versatile text transformation utility that excels at character-level operations like case conversion, character deletion, and text cleanup. It's particularly useful for data preprocessing, file formatting, and stream processing tasks. Whether you're cleaning data files or transforming text output, tr provides a simple yet powerful solution for character-based text manipulation.
