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

How to use the tr command on Linux system

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use the tr command of the Linux system, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The tr command in the Linux system can replace, compress, and delete characters from standard input. It can change one set of characters into another.

Syntax tr [- cdst] [--help] [--version] [first character set] [second character set] tr [OPTION]... SET1 [SET2] parameter description:-c,-complement: invert the setting character. That is, the part that conforms to the SET1 will not be processed, and the rest of the part that does not conform will be converted.

-d,-delete: delete instruction characters

-s,-squeeze-repeats: reduce consecutive repeated characters to a specified single character

-t,-truncate-set1: cut the specified range of SET1 to equal the length set by SET2

-help: displays program usage information

-version: displays the version information of the program itself

Range of character sets:

Character NNN of\ NNN octal value (1 to 3 is octal value character)

\ backslash

\ a Ctrl-G ringtone

\ b Ctrl-H backspace character

\ F Ctrl-L walk to change pages

\ nNew line of Ctrl-J

\ r Ctrl-M enter

\ t Ctrl-I tab key

\ v Ctrl-X horizontal tabs

CHAR1-CHAR2: the assignment of a range of characters from CHAR1 to CHAR2, based on the order of ASCII codes, only from small to large, not from large to small.

[CHAR*]: this is a SET2-specific setting that repeats the specified characters to the same length as SET1

[CHAR*REPEAT]: this is also a special setting for SET2. The function is to repeat the specified characters up to the set number of REPEAT (the number of REPEAT is calculated in octal system, starting with 0)

[: alnum:]: all alphabetic characters and numbers

[: alpha:]: all alphabetic characters

[: blank:]: all horizontal spaces

[: cntrl:]: all control characters

[: digit:]: all numbers

[: graph:]: all printable characters (excluding spaces)

[: lower:]: all lowercase letters

[: print:]: all printable characters (including spaces)

[: punct:]: all punctuation characters

[: space:]: all horizontal and vertical spaces

[: upper:]: all capital letters

[: xdigit:]: all hexadecimal numbers

[= CHAR=]: all match the specified characters (the CHAR in the equal sign represents the characters you can customize)

Case demonstration: convert input characters from uppercase to lowercase:

Echo "HELLO WORLD" | tr 'Amurz', 'Amurz' hello world'A-Z' and 'Amurz' are collections, and collections can be made on their own. For example, 'ABD-}', 'bB.,',' 'Amuri h' and' a-c0-9 'all belong to collections. You can use'\ n','\ tones' and other ASCII characters in the collection.

Use tr to delete characters:

Echo "hello 123 world 456" | tr-d'0-9 'hello world converts tabs to spaces:

Cat text | tr'\ t''character set complement, which removes all characters from the input text that are not in the complement set:

Echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr-d-c'0-9\ n'1 234 in this example, the complement contains the number 0x9, space, and newline character\ n, so it is not deleted, all other characters are deleted.

By compressing characters with tr, you can compress characters that are repeated in the input:

Echo "thissss is a text linnnnnnne." | tr-s' sn' this is a text line. Ingenious use of tr to add numbers:

Echo 1 2 3 4 5 6 7 8 9 | xargs-N1 | echo $[$(tr'\ n''+') 0] Delete the'^ M' character "caused" by the Windows file:

Cat file | tr-s "\ r"\ n" > new_file or cat file | tr-d "\ r" > the character classes that new_filetr can use:

[: alnum:]: letters and numbers [: alpha:]: letters [: cntrl:]: control (non-printing) characters [: digit:]: numbers [: graph:]: graphic characters [: lower:]: lowercase characters [: print:]: printable characters [: punct:]: punctuation marks [: space:]: blank characters [: upper:]: uppercase letters [: xdigit:]: hexadecimal characters:

Tr'[: lower:]'[: upper:] 'these are all the contents of this article entitled "how to use tr commands in Linux system". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.

Share To

Development

Wechat

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

12
Report