Linux tr Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux tr command for text processing and editing. The tr command is a powerful tool that allows you to translate, delete, squeeze, and complement characters in text. You will start by understanding the basic syntax and usage of the tr command, then explore practical examples of translating and deleting characters, as well as squeezing and complementing characters. This lab provides a comprehensive guide to help you effectively utilize the tr command for your text processing needs.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") linux/TextProcessingGroup -.-> linux/tr("`Character Translating`") subgraph Lab Skills linux/cat -.-> lab-422963{{"`Linux tr Command with Practical Examples`"}} linux/echo -.-> lab-422963{{"`Linux tr Command with Practical Examples`"}} linux/grep -.-> lab-422963{{"`Linux tr Command with Practical Examples`"}} linux/sed -.-> lab-422963{{"`Linux tr Command with Practical Examples`"}} linux/tr -.-> lab-422963{{"`Linux tr Command with Practical Examples`"}} end

Understand the tr Command

In this step, you will learn about the tr command in Linux, which is used for translating, deleting, squeezing, and complementing characters in text.

The basic syntax of the tr command is:

tr [OPTION] SET1 [SET2]

Where:

  • SET1 is the set of characters to be translated or deleted.
  • SET2 is the set of characters to translate SET1 to.
  • OPTION can be one of the following:
    • -c, --complement: Use the complement of SET1.
    • -d, --delete: Delete characters in SET1 from the input.
    • -s, --squeeze-repeats: Replace each sequence of a repeated character (that is in the SET1) with a single occurrence of that character.
    • -t, --truncate-set1: Truncate SET1 to the length of SET2.

Let's start with a simple example to understand the basic usage of the tr command.

echo "Hello, World!" | tr 'a-z' 'A-Z'

Example output:

HELLO, WORLD!

In this example, the tr command translates all lowercase letters in the input to their uppercase counterparts.

Translate and Delete Characters Using tr

In this step, you will learn how to use the tr command to translate and delete characters in text.

First, let's try translating characters. Suppose you have a file with both uppercase and lowercase letters, and you want to convert all the letters to lowercase.

echo "Hello, World!" | tr 'A-Z' 'a-z'

Example output:

hello, world!

In this example, the tr command translates all uppercase letters in the input to their lowercase counterparts.

Now, let's try deleting characters. Suppose you have a file with some unwanted characters, and you want to remove them.

echo "Hello, World!" | tr -d ',' '!'

Example output:

Hello World

In this example, the tr command deletes the comma (,) and exclamation mark (!) characters from the input.

Squeeze and Complement Characters with tr

In this step, you will learn how to use the tr command to squeeze and complement characters in text.

Squeezing characters means replacing each sequence of a repeated character with a single occurrence of that character. For example, if you have a file with multiple consecutive spaces, you can use tr to squeeze them.

echo "Hello   World!" | tr -s ' '

Example output:

Hello World!

In this example, the tr command with the -s (squeeze-repeats) option replaces the multiple consecutive spaces with a single space.

Complementing characters means using the complement of the specified character set. For example, if you have a file with both uppercase and lowercase letters, and you want to extract only the uppercase letters, you can use the -c (complement) option.

echo "Hello, World!" | tr -c 'A-Z' ''

Example output:

HW

In this example, the tr command with the -c option extracts only the uppercase letters from the input.

Summary

In this lab, you learned about the tr command in Linux, which is used for translating, deleting, squeezing, and complementing characters in text. You started by understanding the basic syntax and usage of the tr command, and then learned how to translate and delete characters. Finally, you explored how to squeeze and complement characters using the tr command. The key learning points include translating characters to uppercase or lowercase, deleting unwanted characters, squeezing repeated characters, and using the complement of a character set.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like