Book Image

Linux Shell Scripting Essentials

Book Image

Linux Shell Scripting Essentials

Overview of this book

Table of Contents (15 chapters)
Linux Shell Scripting Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Character-based translation using tr


Another interesting shell command is tr. This translates, squeezes, or deletes characters from the standard input. The syntax will be as follows:

tr [OPTION]... SET1 [SET2]

The options for the tr commands are explained in the following table:

Option

Description

-c, -C

Use complement of SET1

-d

This deletes a range of characters specified in SET1.

-s

This replaces consecutive multiple occurrences of characters in SET1 with a single occurrence.

-t

This truncates SET1 to the length of SET2. Any extra characters in SET1 will be not considered for translation.

SETs are a string of characters that can be specified using the following:

  • A character class: [:alnum:], [:digit:], [:alpha:] and so on

  • A character range: 'a-z', 'A-Z', and '0-9'

  • An escape character: \\, \b, \r, \n, \f, \v, and \t

To provide an input text from a file and an output to a file, we can use the file redirection operators: < (less than for input) and > (greater than for...