Tr command usage

Source: Internet
Author: User
Tags character classes
ArticleDirectory
    • Purpose
    • Syntax
    • Description
    • Flag
    • Exit status
    • Example

Original article link

 

# Echo "ABCD" | tr 'a B' Bbcd

 

Tr command

Purpose

Conversion character.

Syntax

Tr[-C|-CDs|-CS|-C|-CDs|-CS|-DS|-S] [-]String1 String2

Tr{-CD|-CS|-CD|-CS|-D|-S}[-]String1

Description

TrThe command deletes or replaces characters from the standard input and writes the results to the standard output. AccordingString1AndString2The string specified by the variable and the specified flag,TrThe command can perform three operations.

Conversion character

IfString1AndString2Both are specified,-DIf the flag is not specifiedTrThe command willString1Replace each character inString2In the same position.

Use the-D flag to delete characters.

If-DIf the flag has been specifiedTrThe command is deleted from the standard input.String1Contains every character.

Remove sequence with-S Mark

If-SIf the flag has been specifiedTrThe command will removeString1OrString2All characters except the first character in any string series. ForString1Every character in,TrThe command removes all characters except the first occurrence from the standard output. ForString2Every character in,TrThe command removes all characters except the first occurrence of the Character Sequence in the standard output.

Represents a special sequence of strings.

String1AndString2The strings contained in the variable can be expressed using the following conventions:

C1-C2 SpecifiedC1The specified characters andC2The string between the specified characters (including C1 and C2.C1The specified characters must be sortedC2Before the specified character.

Note: The current language environment has an important impact on the results when this method is used to indicate the subrange. If you need to use commands to generate consistent results irrelevant to the language environment, avoid using subscopes.
[C*Number] NumberIs an integer that specifiesCThe number of repeated characters. Unless the first digit is 0NumberIt is regarded as a decimal integer. If the first digit is 0, it is considered an octal integer.
[ C *] enter the string with the character specified by C . This option is only used to include the end of a string in string2 , it forces the string in string2 to have the same string as the one specified by the string1 variable number of characters. * (asterisks) are ignored.
[ : classname :] specify all characters in the character class named by classname in the current language environment. The class name can be any of the following:

 alnum lower 
alpha print
blank punct
cntrl space
digit upper
graph xdigit

Except [: lower :] and [: upper :] converts the characters specified by other character classes. In the unspecified order. Because the sequence of characters specified by the character class is not defined, these characters are used only when the objective is to map multiple characters into one. Except for converted character classes.

for more information about character classes, see ctype subroutines.

[=C=] Specify that all characters haveCThe specified character has the same equivalence class.
\Octal Specifies the character, which is encodedOctalThe specified octal value.OctalIt can be a one-, two-, or three-digit octal integer. NULL characters can be used'\ 0'And can be processed as any other character.
\Controlcharacter SpecifyControlcharacterThe control character corresponding to the specified value. It can represent the following values:
\
Warning
\ B
Return key
\ F
Form feed
\ N
Line feed
\ R
Enter
\ T
Tabulation key
\ V
Vertical tabulation key
\\ It indicates that "\" (backslash) is used as a backslash without any special meaning as an escape character.
\[ If you specify "[" (left parenthesis), it is used as the left parenthesis without any special meaning as the start character of a specific string sequence.
\- If "-" is specified, it is used as a negative number without any special meaning as a range separator.

If a characterString1This character is convertedString2AndString1The character corresponding to the last character in.

IfString1AndString2If the specified string length is differentTrThe command ignores the extra characters in a long string.

Flag

- Use the range and character ASCII sorting order, and execute all operations in one byte, not in the current language environment.
-C specify the string1 value to string1 = "italic"> supplemental Code . The complement of string1 is all characters in the character set of the current language environment, except for the characters specified by string1 . If the -A and -C labels are specified, the characters related to all the eight-character Code sets are supplemented. If the -C and -S flag are specified, the -S flag applies to the characters in the complement code of string1 .

If the -D option is not specified, the complement codes of the characters specified by string1 are placed in the ascending array (for example, lc_collate ).

-C specify the string1 value to string1 = "italic"> supplemental Code . The complement of string1 is all characters in the character set of the current language environment, except for the characters specified by string1 . If the -A and -C labels are specified, the characters associated with all 8-character code sets are supplemented. If the -C and -S flag are specified, the -S flag applies to the characters in the complement code of string1 .

If the -D option is not specified, the complement code of the value specified by string1 is placed in an array in ascending order of binary values.

-D Delete from standard input contained inString1Each character in the specified string.

Note:
  1. When-COptions and-DAllString1Other than the specified characters. IgnoreString2, Unless-S.
  2. When-COptions and-DAllString1Other than the specified characters. IgnoreString2, Unless-S.
-S All characters except the first character are removed from the recurring character series. SetString1The specified character sequence is removed from the standard input before conversion, andString2The specified character sequence is removed from the standard output.
String1 Specify a string.
String2 Specify a string.

Exit status

This command returns the following export value:

0 All inputs are processed successfully.
> 0 Generate an error.

Example
  1. To convert braces into parentheses, enter:

    Tr '{} ''()' <textfile> newfile

    In this way{(Left braces)((Left parentheses), and}(Right braces))(Parentheses ). All other characters remain unchanged.

  2. To convert braces into square brackets, enter:
    Tr '{} ''\ []' <textfile> newfile

    In this way{(Left braces)[(Left square brackets), and}(Right braces)](Right square brackets ). The left square brackets must be entered with an escape character.

  3. To convert lowercase letters to uppercase letters, enter:
    Tr 'a-Z' <textfile> newfile
  4. To create a word list in a file, enter:
    Tr-CS '[: lower:] [: Upper:] ''[\ n *]' <textfile> newfile

    This will convert the characters in each sequence (except uppercase and lowercase letters) into a single line break.*(Asterisk) can be usedTrThe command must be repeated multiple times to make the second string as long as the first string.

  5. To delete all null characters from an object, enter:
    Tr-d' \ 0' <textfile> newfile
  6. If you want to replace one or more line breaks of each sequence with separate line breaks, enter:
    Tr-s '\ n' <textfile> newfile

    Or

    Tr-s '\ 012' <textfile> newfile
  7. To use "?" (Question mark) to replace each non-printable character (except valid control characters), enter:
    Tr-C' [: Print:] [: cntrl:] ''[? *] '<Textfile> newfile

    This scans the files created in different language Environments To find the 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:] ''[# *]'

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.