TR Command Usage

Source: Internet
Author: User
Tags character classes

Original link

# echo "ABCD" |tr ' a ' B 'BBCD

TR command

Use

Convert characters.

Grammar

TR [- C | -cds | -cs | - C | -cds | -cs | -ds | - s ] [ -A ] String1 String2

TR { -CD | -cs | -CD | -cs | - D | - S } [ -A ] String1

Describe

The TR command removes or replaces characters from standard input and writes the results to standard output. The TR Command performs three operations based on the string specified by the String1 and String2 variables and the specified flags.

Convert characters

If both String1 and String2 are specified, but the- D flag is not specified, the TR command replaces every word contained in STRING1 in the standard input with the character nonalphanumeric the same position in String2.

Use the-D flag to delete characters

If the- d flag is specified, the TR command removes every character contained in String1 from standard input.

To remove a sequence with the-s flag

If the -s flag is specified, the tr command drops all characters except the first character in any string series contained in String1 or String2. For each character contained in the STRING1, theTR command removes all characters from the standard output except for the first occurrence of the character. For each character contained in the String2, theTR command removes all characters except the first occurrence of a character sequence in the standard output.

Special sequence of expression strings

The strings contained in the STRING1 and String2 variables can be represented using the following conventions:

C1-C2 A string that specifies the collation of characters specified by C1 and the characters specified by C2, including C1 and C2. The characters specified by C1 must be sorted before the characters specified by C2. Note: When using this method to specify a child scope, the current locale has a significant effect on the result. If you need to use commands to produce consistent results unrelated to the locale, you should avoid using child scopes.
[C*number] Number is an integer that specifies how many times the characters specified by C are repeated. Number is treated as a decimal integer unless the first digit is 0, or an octal integer if the first digit is 0.
[c*] with  < Span class= "Italic" >c  the specified character to fill in the string. This option is used only at the end of the string contained in the  string2 , which forces the  string2  to have a string with the  string1  variable. Any characters specified after * (asterisk) are ignored.
[ :classname:] specified by  classname   All characters in the character class that are named. The class name can be any of the following names:
alnum lower 
Alpha print
Blank punct
Cntrl SPAC e
Digit Upper
graph xdigit

Except  [:lower :]  and  [:upper :]  convert character classes, The characters specified by other character classes are placed in the array in an unspecified order. Because the order of the characters specified by the character class is undefined, these characters are used only when the purpose is to map multiple characters to one. Convert character classes except.

For more information about character classes, see  ctype  subroutine.

[=C=] Specifies that all characters have the same equivalence class as the character specified by C.
\Octal Specifies the character whose encoding is represented by the octal value specified by octal. the octal can be a 1-bit, 2-bit, or 3-bit octal integer. The null character can be expressed as ' \ s ' and can be processed like any other character.
\Controlcharacter Specifies the control character corresponding to the value specified by the Controlcharacter. can represent the following values:
\a
Warning
\b
Backspace key
\f
Page change
\ n
Line break
\ r
Enter
\ t
TAB key
\v
Vertical Tab key
\\ The rule "\" (backslash) is used as a backslash, without any special meaning as an escape character.
\[ Specifies that the "[" (opening parenthesis) is used as an opening parenthesis, and does not have any special meaning as the starting character of a particular string sequence.
\- Specifies that the "-" (minus sign) is used as a minus, without any special meaning as a range delimiter.

If a character is specified more than once in String1, the character is converted to a character typeface corresponding to the last word in the String1 in String2.

If the string lengths specified by String1 and String2 are not the same, the TR command ignores extra characters in a longer string.

Sign

-A Use the range and character classes in ASCII collation order, one byte at a byte to perform all operations, rather than using the current locale collation order.
-c Specify  string1  value  string1  the string specified by complement replacement. string1  is the complement of all characters in the current locale's character set, -a  and the  -c  flag are specified, the characters associated with all 8-character code sets will be replenished. class= >-s  "Bold"  -c  and  -s  flag are specified The flag applies to characters in the complement of the  string1 .

If  < is not specified Span class= "bold" >-d  option, the complement of characters specified by  string1  will be placed in an ascending array (e.g.   lc_collate ).

-c Specify  string1  value  string1  the string specified by complement replacement. string1  is the complement of all characters in the current locale's character set, -a  and the  -c  flag are specified, the characters associated with all 8-character code sets will be replenished. class= >-s  "Bold"  -c  and  -s  flag are specified The flag applies to characters in the complement of the  string1 .

If  < is not specified Span class= "bold" >-d  option, the complement of values specified by  string1  will be placed in an array arranged in ascending order of binary values.

-D Deletes each character contained in the string specified by String1 from standard input . Note:
  1. When the- C option is specified together with the-D option, all characters except those specified by String1 are removed. Ignores the contents of the String2 unless the- s option is also specified.
  2. When the- C option is specified together with the-D option, all characters except those specified by String1 are removed. Ignores the contents of the String2 unless the- s option is also specified.
-S Removes all characters except the first character in a repeating character sequence. Removes the character sequence specified by String1 from the standard input before conversion, and removes the sequence of characters specified by String2 from the standard output.
String1 Specifies a string.
String2 Specifies a string.

Exit status

The command returns the following exit values:

0 All input processing is successful.
>0 Generates an error.

Example
  1. To convert curly braces to parentheses, enter:
    Tr ' {} ' () ' < textfile > NewFile

    This converts each {(left curly brace) into ((left parenthesis) and converts each }(the closing brace) into a (right parenthesis) . All other characters remain the same.

  2. To convert the curly braces to Solutionkeys brackets, enter:
    Tr ' {} ' \[] ' < textfile > NewFile

    This converts each {(opening brace) to [(left parenthesis) and converts each }(closing brace) to ](right parenthesis). The opening parenthesis must be entered along with a "\" (anti-skew) escape character.

  3. To convert lowercase characters to uppercase, enter:
    Tr ' A-Z ' A-Z ' < textfile > NewFile
  4. To create a list of words in a file, enter:
    Tr-cs ' [: lower:][:upper:] ' [\n*] ' < textfile > NewFile

    This converts each sequence of characters (except large, lowercase letters) to a single line break. *(asterisk) allows the TR command to repeat a newline character multiple times to make the second string as long as the first string.

  5. To remove all empty characters from a file, enter:
    Tr-d ' < textfile > NewFile
  6. To replace one or more line breaks for each sequence with a separate newline, enter:
    Tr-s ' \ n ' < textfile > NewFile

    Or

    Tr-s ' \012 ' < textfile > NewFile
  7. To use the? (question mark) replaces each nonprinting character (except for the valid control character), enter:
    Tr-c ' [:p Rint:][:cntrl:] ' [? *] ' < textfile > NewFile

    This scans the files created in the different locales to find characters that cannot be printed in the current language environment.

  8. To replace each character sequence in the <space> character class with a single "#" character, enter:
    Tr-s ' [: Space:] ' [#*] '

TR Command Usage

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.