Centos command -- tr

Source: Internet
Author: User

Tr is used to convert characters from standard input through replacement or deletion. Tr is mainly used to delete control characters in a file or convert characters.
Note: tr can only replace, contract, and delete characters, but cannot replace strings.

Tr command format:

Tr-c-d-s ["string1_to_translate_from"] ["string2_to_translate_to"] file
Here:
-C: replace this character set with the supplement set of Character Set 1. the character set must be ASCII.
-D: delete all input characters in string 1.
-S: deletes all recurring character sequences. Only the first string is retained. The duplicate string is compressed into one string.
File: conversion file name. Although other formats can be used for input, this format is the most commonly used.

Character range:

When specifying the content of string 1 or string 2, you can only use a single character or string range or list.
A string consisting of characters in a-z.
[A-Z] A string consisting of characters within the A-Z.
[0-9] numeric string.
\ Octal is a three-digit octal number, which corresponds to valid ASCII characters.
[O * n] indicates the repeated occurrence of the character O for a specified number of times. Therefore, [O * 2] matches the OO string.
Different expressions of specific control characters in tr
Shorthand meaning octal
\ A Ctrl-G ringtone \ 007
\ B Ctrl-H return character \ 010
\ F Ctrl-L line feed \ 014
\ N Ctrl-J New Line \ 012
\ R Ctrl-M press enter \ 015
\ T Ctrl-I tab key \ 011
\ V Ctrl-X \ 030

Example

(1 remove repeated lower-case characters in oops.txt (#-s retains the first character)

[Root @ localhost ~] # Cat oops.txt
Ddddfffabccccc
Lerrrrdddd
[Root @ localhost ~] # Tr-s "[a-z]" <oops.txt> result.txt
[Root @ localhost ~] # Cat result.txt
Dfabc
Lerd

(2) Delete empty rows

[Root @ localhost ~] # Cat oops.txt
Ddddfffabccccc




Lerrrrdddd
[Root @ localhost ~] # Tr-s "[\ 012]" <oops.txt> result.txt
[Root @ localhost ~] # Cat result.txt
Ddddfffabccccc
Lerrrrdddd

(3) Delete all empty rows

[Root @ localhost ~] # Cat oops.txt
Ddddfffabccccc




Lerrrrdddd
[Root @ localhost ~] # Tr-d "[\ 012]" <oops.txt> result.txt
[Root @ localhost ~] # Cat result.txt
Ddddfffabccccclerrrrdddd

(4) small to uppercase

[Root @ localhost ~] # Cat oops.txt
Ddddfffabccccc
Errrrdddd
[Root @ localhost ~] # Cat oops.txt | tr "[a-z]" "[A-Z]"> result.txt
[Root @ localhost ~] # Cat result.txt
DDDDFFFABCCCCC
ERRRRDDDD


(5) Delete the specified characters (#-d is different from-s,-d is deleted, but-s retains the first character)

[Root @ localhost ~] # Cat oops.txt
Ddddfffabccccc
Errrrdddd
[Root @ localhost ~] # Cat oops.txt | tr-d "[bd]"> result.txt
[Root @ localhost ~] # Cat result.txt
Fffaccccc
Errrr
[Root @ localhost ~] # Cat oops.txt | tr-s "[bd]"> result.txt
[Root @ localhost ~] # Cat result.txt
Dfffabccccc
Errrrd

(6) Replace the specified character (# One-to-one replacement)

[Root @ localhost ~] # Cat oops.txt
Ddddfffabccccc
Errrrdddd
[Root @ localhost ~] # Cat oops.txt | tr "[bd]" "[BD]"> result.txt
[Root @ localhost ~] # Cat result.txt
DDDDfffaBccccc
ErrrrDDDD

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.