Linux tr command and linuxtr command

Source: Internet
Author: User

Linux tr command and linuxtr command
Introduction

The tr command can replace, compress, and delete characters from standard input. It can convert a group of characters into another group of characters.


Syntax

Tr [OPTION]... SET1 [SET2]

Note: SET2 is optional

OPTION:

Without parameters: replace each character in SET2 with each character in SET1, which is a sequential replacement. If the length of SET1 is greater than SET2, replace the characters in SET1 with the last character in set2. -T: replace each character in SET2 with each character in SET1. The character sequence is 1-to-1. regardless of the length of SET1 or SET2, only the corresponding character is replaced. -C: replace any character other than the character specified in SET1 with the character specified in set2. if SET2 contains multiple characters, it must be replaced with the last character. -D: Delete the character specified in SET1, where there is no SET2-s: Replace the continuous repeating character specified in SET1 with a single character.

 

Character Set code:

[: Alnum:]: letters and numbers, can be used to replace 'a-zA-Z0-9 '[: alpha:]: letters, can be used to replace 'a-zA-Z '[: cntrl:]: control (non-print) character [: digit:]: Number, can be used to replace '0-9' [: graph:]: graphic character [: lower:]: lowercase letters, which can be used to replace 'a-Z' [: print:]: printable character [: punct:]: punctuation [: space:]: blank character [: upper:]: uppercase letters, which can be used to replace 'a-Z' [: xdigit:]: hexadecimal characters

 

\ Backslash \ a terminal announcement \ B backspace \ f form feed \ n line feed \ r press enter \ t horizontal tab \ v vertical Tab
\ 0 null Character

 

 

 

Example:

1. Replace SET2 with SET1 without parameters, and the length of SET1 is greater than SET2

[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr 'abc' '12'11AA122BB222CC3

A is replaced with 1, B is replaced with 2, and c is replaced with 2

2. Replace SET2 with SET1 without parameters, and the length of SET1 is smaller than SET2

[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr 'ab' '123'11AA122BB2ccCC3

A is replaced with 1, B is replaced with 2

3.-t Parameter

[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr -t 'abc' '12'11AA122BB2ccCC3[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr -t 'ab' '123'11AA122BB2ccCC3

Both a and B are replaced with 2.

4. Delete the specified character,-d

[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr -d 'a-z' AA1BB2CC3[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr -d -c 'a-z\n'  aabbcc

The first is to delete lower-case characters, and the second is to delete other characters except lower-case characters. The following uses the character set to achieve the same effect.

[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr -d '[:lower:]' AA1BB2CC3[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr -d -c '[:lower:]\n'aabbcc

5. Replace consecutive characters with-s

[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr -s 'a-zA-Z'aA1bB2cC3[root@localhost ~]# echo "aaAA1bbBB2ccCC3" | tr -s '[:alnum:]\n'aA1bB2cC3

Both of the preceding methods replace repeated characters with a single character.

 

Other usage: replace null characters with line breaks

[root@localhost ~]# cat /proc/4518/environ \nTERM=xtermPATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/mysql/binPWD=/usr/local/mysqlSHLVL=2OLDPWD=/_=/usr/local/mysql/bin/mysqld_safecat: n: No such file or directory
[root@localhost ~]# cat /proc/4518/environ |tr '\0' '\n'TERM=xtermPATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/mysql/binPWD=/usr/local/mysqlSHLVL=2OLDPWD=/_=/usr/local/mysql/bin/mysqld_safe[root@localhost ~]#

Split a sentence into multiple rows.

Summary

There is a misunderstanding that it is easily understood as SET1 by mistake. SET2 is a character combination, but this is not the case. Both SET1 and SET2 are replacement between single characters of values, for example, 'AB' should not regard AB as a combination.

 

 

Note:

Author: pursuer. chen

Blog: http://www.cnblogs.com/chenmh

All essays on this site are original. You are welcome to repost them. However, you must indicate the source of the article and clearly give the link at the beginning of the article.

Welcome to discussion

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.