In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge about how to use the tr command. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
By using tr, you can easily implement many of the most basic functions of sed. You can think of tr as a (extremely) simplified variant of sed: it can replace one character with another, or it can remove some characters altogether. You can also use it to remove duplicate characters.
The common options for the command format tr [option] ["string1"] ["string2"] are:
Default option. When there is no option, tre defaults to the replace operation, which is to replace the characters that appear in string1 in the file with those in string2. -c option to replace string1 with a complementary set of characters in string1, where the character set is ASCII. The-d option deletes all characters in the file that appear in string1. The-s option removes characters that are repeated in the file and appears in the string1, leaving only one. The-c option, when used, simply replaces string1 with the current complement, such as when using the
Inuxidc@Ubuntu:~/linuxidc.com$ echo "hello world,linuxidc.com,2019" | tr-c "0-9"*" * 2019 linuxidc.com $echo "hello world,linuxidc.com,2019" | tr "0-9"*" hello world,linuxidc.com,****
As you can see, we use 0-9, and when we add the-c option, we replace 0-9 with its complement, which naturally does not contain 0-9, but contains a lot of other characters, and then replaces all other characters with the * sign, but not the number.
The value range of a string
Only a single character or string range or list can be used when specifying the contents of a string or string2. A string consisting of characters within [amurz] amerz. [Amurz] A string consisting of characters within Amurz. [0-9] numeric string. \ octal A three-digit octal number that corresponds to a valid ASCII character. [On] indicates that the character O is repeated a specified number of times n. So [O2] matches the string of OO.
Different expressions of control characters
Shorthand meaning octal mode\ a Ctrl-G ringtone\ 007\ b Ctrl-H backspace\ 010\ f Ctrl-L walk page feed\ 014\ n Ctrl-J new line\ 012\ r Ctrl-M enter\ 015\ t Ctrl-I tab key\ 011\ v Ctrl-X\ 030 pay attention to these control characters. If you want to enter under linux, if we may need to enter ^ M, just press ctrl+V+M at the same time.
Character substitution linuxidc@ubuntu:~/linuxidc.com$ echo "hello world,www.linuxidc.com" | tr "Amurz"Amurz"Amurz" HELLO WORLD,WWW.LINUXIDC.COMlinuxidc@ubuntu:~/linuxidc.com$ echo "hello world,www.linuxidc.com" | tr "Amurl"Amurz" HELLo worLD,www.LInuxIDC.Comlinuxidc@ubuntu:~/linuxidc.com$ echo "hello world,www.linuxidc.com" | tr "amurz"Amurh" HEHHH HHHHD,HHH.HHHHHHDC.CHH
The first line of output is to replace lowercase with uppercase. The second line of output replaces the lowercase Amurl with Amurl respectively, while the characters after the lowercase l are not replaced. The third line of output changes the lowercase Amurh to Amurh, and all the characters after h are replaced by H, because the replacement space of the latter is not as large as the previous character space, so repeat the following H, which is equivalent to the following character is A-HHH. HHHHH .
If we want to convert case to case, we can press the following input:
Tr "Amurz"Amurz" removes repetitive characters
At this point, the option used is the-s option, such as:
Linuxidc@ubuntu:~/linuxidc.com$ echo "root,hello world,linuxidc.com" | tr-s "ao" rot,hello world,linuxidc.comlinuxidc@ubuntu:~/linuxidc.com$ echo "root,hello world,linuxidc.com" | tr-s "lo" rot,helo world,linuxidc.comlinuxidc@ubuntu:~/linuxidc.com$ echo "root,hello world,linuxidc.com" | tr-s "a murz" rot,helo world,linuxidc.comlinuxidc@ubuntu:~/linuxidc.com$ echo "root,hello world Linuxidc.com "| tr-s" 0-9 "root,hello world,linuxidc.com
The first line means to remove the repetitive characters contained in the "ao" character set from the input string, leaving only one. Because of "hello world,linuxidc.com", only o satisfies the condition, so turn linuxidc.com into rot and the middle two o into one. The second line compresses both hello and linuxidc.com characters. The third line says to remove all the characters except the complex characters in amurz. The third line means that the repeated characters in the string and the repeating characters in the 0-9 character set are removed, which is not found here.
If we want to get rid of blank lines, we can do this:
Tr-s "\ n"\ 012"
Is to remove the duplicate newline characters and leave only one.
Delete character
The-d option is similar to the-s option, except that the-d option removes all characters that appear.
Linuxidc@ubuntu:~/linuxidc.com$ echo "root,hello world,linuxidc.com" | tr-d "a murh" root,llo worl,linuxi.omlinuxidc@ubuntu:~/linuxidc.com$ echo "root,hello world,linuxidc.com,2019" | tr-d "a murz", 2019 linuxidc.com $echo "root,hello world,linuxidc.com,2019" | tr-d "0-9" root,hello world,linuxidc.com That's all of the article "how to use tr commands" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.