JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Linux character conversion command tr

Author:JIYIK Last Updated:2025/04/08 Views:

This article introduces the character conversion command tr. Tr is a pipeline command that can be used to delete text in a message or replace text information!

tr [options] ... SET1 [SET2]

The commonly used options of tr are as follows

-d delete the string SET1 in the information

# echo root:x:0:0:root:/root:/bin/bash | tr –d root
:x:0:0::/:/bin/bash

We see that the root has been deleted.

-s removes repeated characters


# echo root:x:0:0:root:/root :/bin/bash | tr –s [o] rot:x:0:0:rot:/rot:/ bin/bash

When -d is not used and SET1 and SET2 appear in the command at the same time, it means that SET1 is replaced by SET2.

# echo root:x:0:0:root:/root:/bin/bash | tr [o] [s]
rsst:x:0:0:rsst:/rsst:/bin/bash

We can see that o is replaced by s. If we use it with option -s, it will remove duplicates first and then replace.

# echo root:x:0:0:root:/root:/bin/bash | tr –sos
rst:x:0:0:rst:/rst:/bin/bash

It should be noted here that when the length of the SET1 string is greater than the length of SET2, tr will repeatedly append the last character of SET2 until it reaches the length of SET1.

# echo root:x:0:0:root:/root:/bin/bash | tr bash bin
root:x:0:0:root:/root:/bin/binn

Let's see if the result is to output the last character n of bin repeatedly.

Another situation is that if there are repeated characters in SET1, the corresponding character will be found in SET2 according to the position of the last character of the repeated characters in SET1, and all the repeated characters in SET1 will be replaced with this character.

# echo root:x:0:0:root:/root:/bin/bash | tr root bin

Now let's not rush to tell the result, let's analyze it step by step. For r in the first position, it is replaced by b in the first position of bin, which is no problem. For o in the second position, it should normally correspond to i in the second position of bin. But because o is repeated, the last o is the third position, so it corresponds to n in the third position of bin. Therefore, both o should be replaced by n. T in the fourth position is very simple, because bin has only three digits, so t is also replaced by the last digit. So the result of the above command should be to replace root with bnnn.

bnnn:x:0:0:bnnn:/bnnn:/bin/bash

Yes, that's the result. Well, I believe I have made it clear. Let's look at an example using regular expressions:

# echo root:x:0:0:root:/root:/bin/bash | tr [az] [AZ] //Convert all lowercase letters to uppercase
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH

That's all about the tr command. You can use man tr to view more information about regular expressions in tr.

I hope this article is helpful to you.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Restart PostgreSQL in Ubuntu 18.04

Publish Date:2025/04/09 Views:72 Category:PostgreSQL

This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n

Issues to note when installing Apache on Linux

Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM

As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial