In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
01. Command Overview
The tr command replaces, compresses, and deletes characters from standard input. It can change one set of characters into another, and is often used to write beautiful one-line commands.
The English full name of tr is "transform", which means transformation. Tr he can only read data from standard input, so tr either redirects the input file to standard input or reads the data from the pipeline.
Note: tr is similar to the sed command, but simpler than sed, so sed can implement everything tr can do.
02. Command format
Usage: tr [option]. SET1 [SET2]
03. Common option
Replace, reduce, and / or delete characters from standard input, and write the results to standard output.
-C,-C,-- complement first complements SET1
-d,-- delete deletes content that matches SET1 and does not replace it
-s,-- squeeze-repeats if the character matching SET1 has a continuous
Repeat, which is uniformly reduced to the length of one character when replaced
-t,-- truncate-set1 first truncates the length of SET1 to be equal to SET2
-- help displays this help and exits
-- version displays version information and exits
A SET is a set of strings that can generally be taken literally. The parsing sequence is as follows:
\ NNN characters with octal values of NNN (1 to 3 digits)
\\ backslash
\ a terminal beeps
\ b backspace
\ f Page change
\ nWrap
\ r enter
\ t horizontal tabs
\ v Vertical tab character
Character 1-all characters experienced in the ascending increment of character 1 to character 2.
[character *] applies in SET2. The specified character is copied continuously until it matches the length of setting 1.
[character * times] perform a specified number of copies of a character. If the number begins with 0, it is regarded as an octal number.
[: alnum:] all letters and numbers
[: alpha:] all the letters
[: blank:] all white space characters arranged horizontally
[: cntrl:] all control characters
[: digit:] all the numbers
[: graph:] all printable characters, excluding spaces
[: lower:] all lowercase letters
[: print:] all printable characters, including spaces
[: punct:] all punctuation characters
[: space:] all white space characters arranged horizontally or vertically
[: upper:] all capital letters
[: xdigit:] all hexadecimal numbers
[= character =] all characters equal to a specified character
The replacement occurs only if both SET1 and SET2 are given and there is no-d option.
The-t option is only possible when replacing. If needed, SET2 will be added to the end by adding the original last character.
Add to be as long as SET1. Extra characters in SET2 will be omitted. Only [: lower:] and [: upper:]
Unfolds characters in ascending order; indicates case conversion in pairs in the SET2 used for substitution. -s acts on SET1, neither
Replacement is also not deleted, otherwise SET2 reduction is used after replacement or expansion.
Character range
Only a single character or string range or list can be used when specifying the contents of string 1 or string 2.
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.
[Olymn] indicates that the character O is repeated a specified number of times n. So [Olympian 2] matches the string of OO.
Different expressions of specific control characters in tr
Shorthand meaning octal mode
A Ctrl-G ringtone\ 007
\ b Ctrl-H backspace character\ 010
\ F Ctrl-L walk to change pages\ 014
\ nNew line of Ctrl-J\ 012
\ r Ctrl-M enter\ 015
\ t Ctrl-I tab key\ 011
\ v Ctrl-X\ 030
04. Reference exampl
4.1 convert input characters from uppercase to lowercase
[deng@localhost ~] $echo "HELLO ITCAST" | tr 'AmurZ' a-z'hello itcast [deng@localhost ~] $
Both 'Amurz' and 'Amurz' are collections, and collections can be made on their own. For example, 'ABD-}', 'bB.,'','a murmurh' and 'a-c0-9' all belong to the collection. You can use'\ n','\ ASCII 'and other ASCII characters in the collection.
4.2 Delete the number that appears
[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
[deng@localhost test] $echo aabbcc..#dd2 * / dk4 | tr-d-c'0-9\ nfolk 2 4 [deng@localhost test] $
The complement contains the number 0# 9, space, and newline character\ n, so it is not deleted, and all other characters are deleted.
4.4 represent consecutive repeated characters as a single character
[deng@localhost test] $echo "helloooooooooo is heimamaaaaaaaaaa" | tr-s' oa'hello is heimama [deng@localhost test] $
4.5 perform a + operation using a replace operation
[deng@localhost test] $echo 1 2 3 45 6 7 8 9 | xargs-N1 | echo $[(tr'\ n''+') 0] 45 [deng@localhost test] $
4.6 remove the'^ M 'character caused by the Windows file
[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 newline characters 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 on the use of the Linux tr command, more relevant Linux tr command content, please search the previous articles or continue to browse the following related articles hope that you will support more in the future!
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.