Detailed explanation of linux tr command

        By using tr, you can implement many of sed's most basic functions very easily. You can think of tr as an (extremely) simplified variant of sed: it can replace one character with another, or it can remove some characters entirely. You can also use it to remove duplicate characters. That's all tr ​​can do. 

        tr is used to convert characters from standard input by substitution or deletion. tr is mainly used to delete control characters in a file or perform character conversion. When using tr there are two strings to convert: string 1 for the query and string 2 to handle the various conversions. When tr is just executed, the characters in string 1 are mapped to the characters in string 2, and the conversion operation begins.

1. The tr command format with the most common options is:

tr -c -d -s ["string1_to_translate_from"] ["string2_to_translate_to"] < input-file

here:

        -c Replace this character set with the complement of the character set in string 1, requiring the character set to be ASCII.

        -d Delete all input characters in string 1.

        -s Remove all repeated character sequences, keeping only the first; that is, compress the repeated string into a single string.

        input-file is the transform filename. Although other formats can be used for input, this format is the most commonly used.

 

2. Character range

        When specifying the contents of String 1 or String 2, only a single character or a range of strings or a list can be used.

[az] A string of characters within az.
[AZ] A string of characters within AZ.
[0-9] Numeric string.
\octal A three-digit octal number corresponding to a valid ASCII character.
[O*n] means that the character O is repeated a specified number of times n. So [O*2] matches the string of OO.
Different expressions for specific control characters in tr
Shorthand Meaning Octal Mode
\a Ctrl-G Ringtone\007
\b Ctrl-H Backspace \010
\f Ctrl-L Line feed\014
\n Ctrl-J new line\012
\r Ctrl-M Enter\015
\t Ctrl-I  tab键\011
\v Ctrl-X  \030

 

3. Examples

1. Replace "abc" appearing in file with "xyz"

# cat file | tr "abc" "xyz" > new_file

        [Note] Here, all "a" letters that appear in the file are replaced with "x" letters, "b" letters are replaced with "y" letters, and "c" letters are replaced with "z" letters. instead of replacing the string "abc" with the string "xyz".

 

2. Use the tr command to "unify" letter case

(lowercase --> uppercase)

# cat file | tr [a-z] [A-Z] > new_file

(uppercase --> lowercase)

# cat file | tr [A-Z] [a-z] > new_file

 

3. Replace the numbers 0-9 in the file with aj

# cat file | tr [0-9] [a-j] > new_file

4. Delete the "Snail" characters that appear in the file file

# cat file | tr -d "Snail" > new_file

        [Note] Here, all 'S', 'n', 'a', 'i', 'l' characters that appear in the file will be deleted! Instead of just deleting the occurrences of the "Snail" string.

 

5. Delete the newline '\n' and tab '\t' characters that appear in the file file

# cat file | tr -d "\n\t" > new_file

        Invisible characters must be represented by escape characters, which are unified.

 

6. Delete "continuous" repeating letters, keep only the first one

# cat file | tr -s [a-zA-Z] > new_file

 

7. Remove blank lines

# cat file | tr -s "\n" > new_file

 

8. Remove the '^M' character "caused" by Windows files

# cat file | tr -d "\r" > new_file

        or

# cat file | tr -s "\r" "\n" > new_file

        [Note] Here -s is followed by two parameters "\r" and "\n", replace the former with the latter

 

9. Replace the tab character \011 with the space character \040

# cat file | tr -s "\011" "\040"

 

Article source: http://blog.sina.com.cn/s/blog_58c3f7960100uttl.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326337271&siteId=291194637