Linux命令之:tr

1. 用途:

tr,translate的简写,主要用于压缩重复字符,删除文件中的控制字符以及进行字符转换操作。

2. 语法:

tr [OPTION]... SET1 [SET2]

 

3. 参数:

  -s:squeeze-repeats压缩重复字符,使用SET1中列举的单一字符替换输入流中的重复字符

[root@bogon ~]# echo "aaabbbaacccfdddccc" | tr -s [abcdf]
abacfdc

  -d:删除字符

[root@bogon ~]# echo "a1b2c3z4" | tr -d "[a-z][A-Z]"
1234

  -t:替换字符。将SET1中字符用SET2对应位置的字符进行替换,默认值为-t

[root@bogon ~]# echo "a1b2c3d4" | tr -t [abc] [XYZ]                          #将a转换成X,b转换成Y,c转换成Z
X1Y2Z3d4

 [root@bogon ~]# echo "hello world" |tr -t [:lower:] [:upper:]                #将小写转换成大写
 HELLO WORLD

  -c:complement,用SET2替换SET1中没有包含的字符

[root@bogon ~]# echo "aaabbbccc" | tr -c [ab] "z" | tr -s [z] | tr -c [ab] "\n"   #1.将ab以为的字符替换成z; 2.压缩重复项z; 3.将ab以为的字符替换成换行
aaabbb

 

解析字符串:

       \NNN   character with octal value NNN (1 to 3 octal digits)

       \\     backslash

       \a     audible BEL

       \b     backspace

       \f     form feed

       \n     new line

       \r     return

       \t     horizontal tab

       \v     vertical tab

       CHAR1-CHAR2
              all characters from CHAR1 to CHAR2 in ascending order

       [CHAR*]
              in SET2, copies of CHAR until length of SET1

       [CHAR*REPEAT]
              REPEAT copies of CHAR, REPEAT octal if starting with 0

       [:alnum:]
              all letters and digits

       [:alpha:]
              all letters

       [:blank:]
              all horizontal whitespace

       [:cntrl:]
              all control characters

       [:digit:]
              all digits

       [:graph:]
              all printable characters, not including space

       [:lower:]
              all lower case letters

       [:print:]
              all printable characters, including space

       [:punct:]
              all punctuation characters

       [:space:]
              all horizontal or vertical whitespace

       [:upper:]
              all upper case letters

       [:xdigit:]
              all hexadecimal digits

       [=CHAR=]
              all characters which are equivalent to CHAR

 

posted on 2018-02-15 14:36  近博  阅读(195)  评论(0编辑  收藏  举报

导航