Linux Shell TR Command detailed

Source: Internet
Author: User
Tags control characters printable characters
Linux Shell TR Command detailed

1. Use

A shorthand for tr,translate, which is used to compress repeating characters, delete control characters in a file, and perform character conversion operations.

2. Syntax

TR [OPTION] ... SET1 [SET2]

3. Parameters

3.1-s compressing repeating characters

-s:squeeze-repeats, replaces the corresponding repeating character with the character specified by SET1 (replace each input sequence of a repeated character the is listed in SET1 with A single occurrence of that character)

xiaosi@qunar:~/test$ echo "AAABBBAACCCFDDD" | tr-s [ABCDF]//ABACFD

You can use this feature to delete blank lines in a file, essentially the same as above, by replacing the corresponding repeating character with the character specified by SET1

xiaosi@qunar:~/test$ cat B.txti like Footballfootball is very fun! helloxiaosi@qunar:~/test$ Cat B.txt | tr-s ["\ n"]i like Footballfootball is very fun! Hello

3.2-d deleting characters

-d:delete, deleting all characters specified in SET1, does not convert (delete characters in SET1, does not translate)

xiaosi@qunar:~/test$ echo "A12HJ13FDAADFF" | Tr-d "[a-z][a-z]" 1213xiaosi@qunar:~/test$ echo "a1213fdasf" | tr-d [adfs]1213

3.3 Character substitution

-t:truncate, the characters in the SET1 are replaced with the characters in the SET2 corresponding position, the default is-t

xiaosi@qunar:~/test$ echo "A1213FDASF" | tr-t [AfD] [AFO]//A1213FOASF

The code above converts a to a,f conversion to F,d to O.

You can use this feature to convert large and small letters.

xiaosi@qunar:~/test$ echo "Hello World I Love You" |tr-t [A-z] [A-z]hello World I love youxiaosi@qunar:~/test$ echo HELL O World I Love You "|tr-t [A-z] [A-z]hello World I Love You

You can also use a character set to convert

xiaosi@qunar:~/test$ echo "Hello World I Love your" |tr-t [: Lower:] [: Upper:]hello World I love youxiaosi@qunar:~/test$ EC Ho "HELLO World I Love your" |tr-t [: Upper:] [: Lower:]hello World I Love You

Note:

The character set is as follows

\nnn octal value character NNN (1 to 3 characters of octal value) \ \ backslash \a ctrl-g ringtone \b ctrl-h backspace \f ctrl-l walk-through page \ n ctrl-j new line \ r ctrl-m enter \ t ctrl-i tab \v Ct Rl-x horizontal tab CHAR1-CHAR2 All characters from CHAR1 to CHAR2 in the order of ASCII characters [char*] in SET2, copies of CHAR until length of set1[char*repeat] REP EAT copies of CHAR, REPEAT octal if starting with 0[:alnum:] All letters and numbers [: Alpha:] All Letters [: blank:] Horizontal tab, blank, etc. [: Cntrl:] All control characters [:d I Git:] All the numbers [: graph:] All printable characters, excluding spaces [: lower:] All lowercase characters [:p rint:] All printable character, including spaces [:p UNCT:] All punctuation characters [: space:] All horizontal or vertical blanks [: Upper:] All uppercase letters

3.4-Character complement substitution

-c:complement, replacing characters that are not contained in SET1 with SET2

xiaosi@qunar:~/test$ cat A.txtmonday   09:00tuesday  09:10wednesday 10:11thursday  11:30friday   08:00saturday  07:40sunday   10:00xiaosi@qunar:~/test$ cat a.txt | tr-c "[a-z][a-z]" "#" | tr-s "#" | tr-t "#" \ n "Mondaytuesdaywednesdaythursdayfridaysaturdaysunday

Tr-c "[A-z][a-z]" "#" in the above code means replacing all word nonalphanumeric except the size letter with #.

The above code can be optimized to:

xiaosi@qunar:~/test$ Cat A.txt | Tr-cs "[a-z][a-z]" "\ n" mondaytuesdaywednesdaythursdayfridaysaturdaysunday

Thank you for reading, I hope the tender help to everyone, thank you for the support of this site!

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.