Linux common commands [tr] command

tr - translate or delete characters

tr command to convert the file or delete the characters.

tr instruction to read data from the standard input device, the string after translation, outputs the result to the standard output device.

 

grammar:

  tr [OPTION]... SET1 [SET2]

  TR [- CDST ] [- Help ] [- Version ] [first character set] [Second Character Set]

 

parameter:

  • -c, --complement: Anti-selected set of characters. That is in line with SET1 part without processing the remaining part of the non-compliance before converting
  • -d, --delete: delete character instruction
    • # Delete user several characters 
      [root @ Oldboy Oldboy] # CAT new.txt | TR -d 'user' 
      1-01 
      2-02 
      3-03 
      4-04 
      5-05 # delete the letter 
      [root @ Oldboy Oldboy] # CAT new.txt | TR -d [: Alpha:] 
      1-01 
      2-02 
      3-03 
      4-04 
      5-05 # delete - 
      [root @ Oldboy Oldboy] # CAT new.txt | TR -d -           1user01 
      2user02 
      3user03 
      4user04 
      5user05 # delete numbers 1-9 
      [root @ Oldboy Oldboy] # CAT new.txt | TR -d [1-9] 
      - user0
       - user0
       -
      
      
      
      
      
      
      user0
       - user0
       - user0 
      
      # Delete numbers 0-9 
      [root @ Oldboy Oldboy] # CAT new.txt | TR -d [0-9] 
      - the User
       - the User
       - the User
       - the User
       - the User 
      
      # Delete numbers and - 
      [root @ Oldboy Oldboy] # CAT new.txt | TR -d "[0-9] -" 
      the User 
      the User 
      the User 
      the User 
      the User
      tr -d delete set1

       

  • -s, --squeeze-repeats: reduced continuously repeated characters into individual characters designated
  • -t, --truncate-set1: reduction in the specified range SET1, SET2 is set so that with equal length

Example:

[Oldboy Oldboy the root @] # CAT new.txt 
l- User01
 2- named user02
 3- user03
 4- user04
 5- user05 

# from the standard input file contents, as the character set 
[the root @ Oldboy Oldboy] # TR [1-9 ] [AZ] <new.txt 
A- user0a 
B - user0b 
C - user0c 
D - user0d 
E -user0e

  [root@oldboy oldboy]# cat new.txt|tr [1-9] [a-z]
  a-user0a
  b-user0b
  c-user0c
  d-user0d
  e-user0e

1 will be replaced by a, replacing the 2 b, 3 will be replaced by c, ..., and so one.

 

Case conversion may be achieved by:: [upper] Parameter [lower]:

[root@oldboy oldboy]# cat new.txt|tr [:lower:] [:upper:]
1-USER01
2-USER02
3-USER03
4-USER04
5-USER05
[root@oldboy 

 

Guess you like

Origin www.cnblogs.com/zoe233/p/11808745.html