1024programmer Linux Detailed explanation of tr command under linux system – linux operation and maintenance

Detailed explanation of tr command under linux system – linux operation and maintenance

Today I want to introduce to you a very commonly used command-tr. This command can be used to replace strings, delete specified characters, and compress multiple repeated characters to only one. In addition, this command supports pipelines. From the above function introduction, you should be able to feel that this command is very powerful.

Next, let’s introduce this command:

tr [option] parameter

Common options are as follows:

  • -c followed by a character range, indicating characters other than these characters

  • -d deletes the specified character

  • -s compresses the repeated character to only one

The following passes a few Example to learn how to use this command

String replacement

This is the most basic function, when no option is added, it means string replacement, the command The format is:

tr The original string needs to be replaced with the string

Here, we often use the – symbol, which means continuous meaning. Let’s look at the case:

# String replacement, replace lowercase letters with uppercase letters
 # echo hello, world | tr [a-z] [A-Z]
 HELLO, WORLD

Delete characters

Delete the specified characters through the option -d, and use the -d -c option to retain the specified characters. Let’s look at the case

# Delete the specified character ae
 # echo 'There are apples' | tr -d 'ae'
 Thr r ppls

 # Delete the specified characters, only keep numbers, letters and line breaks, and delete all other characters
 # echo 'sSwd,aw23e;sw aswe' | tr -d -c 'a-zA-Z0-9\n'
 sSwdaw23eswaswe

Compressed characters

This function is very commonly used, it can be used to compress consecutively repeated characters into only one. We often use it to delete consecutive spaces and leave only one space, and delete consecutive newlines to leave only one newline. To complete the function of compressing characters, you need to use the -s option.

# Delete consecutive spaces and leave only one (we also often delete consecutive newlines and leave only one)
 # echo -e "hello world.\n\n\n" | tr -s ' \n'
 hello world.

The above examples are relatively simple, and the power of this command may not be seen. Let’s look at a few more complicated cases.

We know that cut is not very good at dealing with continuous spaces. If you want to use cut to select the second column of the result displayed by the df command, it is impossible. At this time, if you first use the tr command to compress multiple consecutive spaces into one, then the cut command can meet your needs.

# df -h | tr -s ' \t' | cut -d ' ' -f 2
 size
 40G
 487M
 497M
 497M
 497M
 100M

You can see that through the processing of tr, cut can now obtain the second column of information.

The command tr is very powerful, and its usage is very simple. I hope everyone can master and apply it.

The above is the detailed content of the tr command under the Linux system. For more information, please pay attention to other related articles on 1024programmer.com!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/detailed-explanation-of-tr-command-under-linux-system-linux-operation-and-maintenance/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索