tr

root@node85 ~]# tr --help

Usage: tr [OPTION]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input,
writing to standard output.

  -c, -C, --complement    use the complement of SET1  取代所有不属于第一字符集的字符;
  -d, --delete            delete characters in SET1, do not translate 删除所有属于第一字符集的字符;
  -s, --squeeze-repeats   replace each input sequence of a repeated character 把连续重复的字符以单独一个字符表示;
                            that is listed in SET1 with a single occurrence
                            of that character
  -t, --truncate-set1     first truncate SET1 to length of SET2  先删除第一字符集较第二字符集多出的字符
      --help     display this help and exit
      --version  output version information and exit

SETs are specified as strings of characters.  Most represent themselves.
Interpreted sequences are:

  \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

 

大小写转换

[root@node85 ~]# echo "HELLO WORLD" | tr 'A-Z' 'a-z'
hello world
[root@node85 ~]# echo "HELLO WORLD" | tr [A-Z] [a-z]
hello world
[root@node85 ~]# echo "HELLO WORLD" | tr [:upper:] [:lower:]
hello world

将制表符转换成其他字符

[root@node85 ~]# cat trtest
h       e       l       l       o
w       o       r       l       d
[root@node85 ~]# cat trtest | tr '\t' '='
h=e=l=l=o
w=o=r=l=d

 

字符集补集,从输入文本中将不在补集中的所有字符删除:

[root@node85 ~]# echo aa.,a 1 b#$bb 2 c*/cc 3 ddd 4 | tr -d -c '0-9 \n'
 1  2  3  4

 

压缩指定的重复字符

[root@node85 ~]# echo "helloooooooooo worllllld" | tr -s 'l'
heloooooooooo world

 

删除Windows文件“造成”的'^M'字符:

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

 

posted @ 2017-05-01 02:21  黑色月牙  阅读(343)  评论(0编辑  收藏  举报