How to use Linux tr command

How to use Linux tr command

01. Command Overview

The tr command can replace, compress, and delete characters from standard input. It can transform one set of characters into another set of characters. It is often used to write beautiful single-line commands and is very powerful.

The full English name of tr is “transform”, which means transformation. tr can only read data from standard input, so tr either redirects the input file to standard input or reads data from a pipe.

Note: tr is similar to the sed command, but simpler than sed, so sed can achieve the same functions as tr.

02. Command format

用法:tr [選項]... SET1 [SET2]

03. Common options

Replaces, reduces, and/or deletes characters from standard input and writes the results to standard output.

-c, -C, --complement first complement SET1
-d, --delete Delete the content matching SET1 without replacing it
-s, --squeeze-repeats If characters matching SET1 are present consecutively in the input sequence
Repeated, will be uniformly shortened to one character length when replaced
-t, --truncate-set1 first truncate SET1 to the same length as SET2
--help Display this help message and exit
--version show version information and exit

SET is a set of strings, which can generally be understood literally. The parsing sequence is as follows:

\NNN Character with octal value NNN (1 to 3 digits)
\\ Backslash
\a Terminal beeps
\b Backspace
\f Page break
\n Line break
\r Enter
\t Horizontal tab character
\v vertical tab character
Character 1 - Character 2 All characters in ascending order from character 1 to character 2
[Character*] Applicable in SET2, the specified character will be copied continuously until it matches the length of setting 1
[character*number] Copy the character a specified number of times. If the number starts with 0, it is treated as an octal number.
[:alnum:] all letters and numbers
[:alpha:] all letters
[:blank:] All horizontal whitespace characters
[:cntrl:] All control characters
[:digit:] all digits
[:graph:] All printable characters, excluding spaces
[:lower:] all lowercase letters
[:print:] all printable characters, including spaces
[:punct:] all punctuation characters
[:space:] All horizontal or vertical whitespace characters
[:upper:] all uppercase letters
[:xdigit:] All hexadecimal digits
[=Character=] All characters that are equal to the specified character

Replacement is done only if both SET1 and SET2 are given, and no -d option is given.
The -t option is only possible when replacing. If necessary, SET2 will be padded to the same length as SET1 by adding the original last character at the end. Extra characters in SET2 will be omitted. Only [:lower:] and [:upper:]
Expands characters in ascending order; in pairs in SET2 for replacement, represents case conversion. -s Act on SET1, neither replace nor delete, otherwise use SET2 to reduce after replacement or expansion.

Character Range

When specifying the contents of string1 or string2, you can only use single characters or string ranges or lists.

[az] A string consisting of the characters in az.

[AZ] A string consisting of the characters in AZ.
[0-9] Digit string.

\octal A three-digit octal number corresponding to a valid ASCII character.
[O*n] means character O is repeated a specified number of times n. Therefore [O*2] matches the string OO.

Different ways to express specific control characters in tr Shorthand meaning Octal mode

\a Ctrl-G Ringtone\007
\b Ctrl-H Backspace\010

\f Ctrl-L Line feed\014
\n Ctrl-J New line\012

\r Ctrl-M Enter\015
\t Ctrl-I tab key\011

\v Ctrl-X \030

04. Reference examples

4.1 Convert input characters from uppercase to lowercase

[deng@localhost ~]$ echo "HELLO ITCAST" | tr 'AZ' 'az'
hello itcast
[deng@localhost ~]$ 

'AZ' and 'a-z' are both sets. Sets can be customized. For example, 'ABD-}', 'bB.,', 'a-de-h', and 'a-c0-9' are all sets. You can use '\n', '\t', and other ASCII characters in the set.

4.2 Delete the numbers that appear

[deng@localhost ~]$ echo "hello 1234 itcast 7890" | tr -d '0-9'
hello itcast 
[deng@localhost ~]$ 

4.3 Remove all characters from the input text that are not in the complement set

[deng@localhost test]$ echo aabbcc..#dd2 */dk4 | tr -d -c '0-9 \n'
twenty four
[deng@localhost test]$

The complement contains the numbers 0-9, spaces, and newline characters \n, so they are not deleted, and all other characters are deleted.

4.4 Representing Repeated Characters as a Single Character

[deng@localhost test]$ echo "helloooooooooo is heimamaaaaaaaaaaa" | tr -s 'oa'
hello is heimama
[deng@localhost test]$

4.5 Using the Replace Operation to Perform the + Operation

[deng@localhost test]$ echo 1 2 3 4 5 6 7 8 9 | xargs -n1 | echo $[ $(tr '\n' '+') 0 ]
45
[deng@localhost test]$

4.6 Remove '^M' characters caused by Windows files

[deng@localhost test]$ cat txt | tr -s '\r' '\n' > file
[deng@localhost test]$

or

[deng@localhost test]$ cat txt | tr -d '\r' > file

4.7 Convert lowercase letters to uppercase letters

[deng@localhost test]$ echo "hello itcast" | tr '[:lower:]' '[:upper:]'
HELLO ITCAST
[deng@localhost test]$ 

4.8 Replace line breaks with tabs

[deng@localhost test]$ cat txt | tr '\n' '\t'
 1111 1111 2222 2222 5555 [deng@localhost test]$ 
[deng@localhost test]$

This is the end of this article about how to use the Linux tr command. For more information about the Linux tr command, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Linux commands sort, uniq, tr tools
  • Linux traceroute command usage detailed explanation
  • The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands
  • Trash-Cli: Command-line Recycle Bin Tool on Linux
  • Detailed explanation of strace command for Linux application debugging
  • Detailed explanation of the usage of tree command under Linux
  • Linux shell tr ​​command detailed explanation
  • Detailed explanation of the strings command in Linux
  • One shell command a day Linux text operation series - tree command detailed explanation
  • Use of Linux tr command

<<:  A brief discussion on the role and working principle of key in Vue3

>>:  MySQL dual-machine hot standby implementation solution [testable]

Recommend

Example of how to deploy a Django project using Docker

It is also very simple to deploy Django projects ...

An article teaches you JS function inheritance

Table of contents 1. Introduction: 2. Prototype c...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

Sample code for automatic web page refresh and automatic jump

Automatic web page refresh: Add the following code...

Detailed explanation of Nginx's rewrite module

The rewrite module is the ngx_http_rewrite_module...

Example code for implementing a text marquee with CSS3

Background Here's what happened, Luzhu accide...

How to install Chrome browser on CentOS 7

This article introduces how to install Chrome bro...

A brief analysis of MySQL locks and transactions

MySQL itself was developed based on the file syst...

mysql5.7.21.zip installation tutorial

The detailed installation process of mysql5.7.21 ...

IDEA uses the Docker plug-in (novice tutorial)

Table of contents illustrate 1. Enable Docker rem...

JavaScript Reflection Learning Tips

Table of contents 1. Introduction 2. Interface 3....

Overview of MySQL Statistics

MySQL executes SQL through the process of SQL par...

10 HTML table-related tags

In fact many people will say “I’ve seen that table...

JavaScript implements checkbox selection function

This article example shares the specific code of ...