Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the tr command under the linux system

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what the tr command is under the linux system. I hope you will gain a lot after reading this article. Let's discuss it together.

Next, let's introduce the command:

Tr [option] parameter

Common options are as follows:

-c followed by a range of characters to represent characters other than these characters

-d Delete the specified character

-s compresses repeating characters and leaves only one

Here are a few examples to learn how to use this command

String substitution

This is the most basic function. When no options are added, the string is replaced, and the command format is:

The string to be replaced with the original tr string

Here, we also often use the-symbol, which means continuous meaning. Let's take a look at the case:

# string replacement, replace lowercase letters with uppercase letters # echo hello,world | tr [Amurz] [Amurz] HELLO,WORLD

Delete character

The specified character is deleted by the option-d, and the specified character can be retained by the-d-c option. Let's take a look at the case.

# delete specified characters ae# echo 'There are apples' | tr-d' ae'Thr r ppls# delete specified characters, leaving only numbers, letters and newline characters, and delete all other characters # echo 'sSwd,aw23e;sw aswe' | tr-d-c' a-zA-Z0-9\ n'sSwdaw23eswaswe

Compressed character

This function is very common, it can be used to compress consecutive repeated characters to only one. We often use it to delete consecutive spaces with only one space, and to delete consecutive newline characters with only one newline character. To compress characters, you need to use the-s option.

# deleting consecutive spaces leaves only one (we also often delete consecutive newline characters) # echo-e "hello world.\ n\ n\ n" | tr-s'\ n'hello world.

The above examples are relatively simple, and you may not see the power of this command. Let's take a look at a few more complicated cases.

We know that cut is not very good at dealing with consecutive spaces. If you want to use cut to select the second column of the df command to display the results, it is impossible. At this point, if you first use the tr command to compress multiple consecutive spaces into one, then the cut command will be able to meet the needs.

# df-h | tr-s'\ t' | cut-d''- f 2Size40G487M497M497M497M100M

As you can see, through the processing of tr, cut can now get the second column of information.

After reading this article, I believe you have a certain understanding of what the tr command is under the linux system. If you want to know more about it, welcome to follow the industry information channel. Thank you for your reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report