Home > Text Processing & Search > tr

tr: Character Translation and Deletion Filter

The `tr` command is a filter command used to translate or delete characters from text received via standard input (stdin). It is particularly useful when used with pipes (|) to process the output of other commands.

Overview

As its name suggests, `tr` (translate) translates or substitutes characters. For example, it specializes in simple character-level transformations such as changing lowercase to uppercase, replacing specific characters with others, or converting newline characters to spaces. Unlike `sed` or `awk`, which support complex pattern matching at the line level, `tr` is very fast and efficient for character-level processing.

Key Features

The main features of the `tr` command are as follows:

  • Translates or deletes text character by character.
  • Receives data only through standard input (stdin).
  • Does not use regular expressions.
  • Widely used with pipes (`|`) to process the output of other commands.

Basic `tr` Syntax

The `tr` command generally takes two character sets as arguments.

  • `tr 'set1' 'set2'`: Translates characters in `set1` to characters in `set2` with a 1:1 correspondence.
  • `tr -d 'set1'`: Deletes all characters present in `set1`.

Key Options

Frequently used `tr` command options are grouped by purpose.

1) Functional Options

2) Special Characters & Sets

Generated command:

Try combining the commands.

Description:

`tr` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn the functions of the `tr` command through various usage examples.

Convert Lowercase to Uppercase

echo 'hello world' | tr '[:lower:]' '[:upper:]'

Converts lowercase letters received from standard input to uppercase and prints them.

Delete Specific Characters

echo 'hello   world' | tr -d ' '

Deletes all spaces (` `) from the text.

Convert Newlines to Spaces

cat file.txt | tr '\n' ' '

Converts newline (`\n`) characters in file content to spaces (` `) and outputs it on a single line.

Squeeze Multiple Spaces

echo 'hello     world' | tr -s ' '

Compresses sequences of repeated spaces into a single space. The `-s` option performs the function of squeezing repeated characters.

Convert Uppercase to Lowercase

echo 'HELLO WORLD' | tr 'A-Z' 'a-z'

Converts uppercase letters received from standard input to lowercase and prints them.

Tips & Cautions

Here are some points to consider when using the `tr` command.

Tips

  • `tr` does not directly accept filenames as arguments. You must pass text to standard input using commands like `cat` or `echo`.
  • When passing strings as arguments, it is best to use single quotes (`'`) to prevent the shell from interpreting special characters.
  • `tr` is simpler in functionality compared to `sed` or `awk`, but it has the advantage of being much faster for character-level transformation tasks.

Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands