tr 命令

       

    1. 参数:

  -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
          -c: 取代SET1字符集中没有包含的字符
          -d:  删除SET1字符集中所有的字符,不转换
               tr -d 'a' 输入中含有a字符都会被删除
          -s: 压缩SET1连续重复的字符
               tr -s 'a' 把重复的连续字符处理成一个字符
          -t: 将SET1 替换成SET2, 缺省为-t
               tr -t "abc" "xyz"  <==> tr "abc" "xyz"


 2. 支持的正则表达式:

            
  \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

    例子:

        1. 将/etc/issue文件中的内容转换为大写后保存到/tmp/issue文件中

     tr 'a-z' 'A-Z' </etc/issue>/tmp/issue

        2. 将登录至当前系统上的用户信息中的信息转换为大写后保存至/tmp/who.out

     who | tr '[[:lower:]]' '[[:upper:]]' > /tmp/who.out

        3. 一个linux用户给root发邮件要求邮件标题为"help",邮件正文如下:Hello I am 用户名,the system version is here, please help me to check it, thanks! 

操作系统版本信息(跟tr无关)

     mail -s "help" root <<EOF
     > Hello, I am $(whoami)
     > the system version is here, please help me to check it, thanks
     > $(cat /etc/redhat-release)
     > EOF

        4.将/root/下文件列表,显示成一行并文件名之间用空格隔开

     ls /root | tr '\n' ' '

        5. file1文件的内容为"1 2 3 4 5 6 7 8 9 10" 计算出所有数字的总和

     echo $[`cat file1 | tr ' ' '+'`]
     cat file | tr ' ' '+' | bc

        6. 删除Windows文本文件中的'^m'字符

     tr -d '\r' < a.txt > LinuxTXT

        7.处理字符串"xt.,|1 jr#!$mn 2 c*/fe 3 uz 4", 只保留其中的数字与空格

     echo "xt.,|1 jr#4""mn 2 c*/fe 3 uz 4" | tr -d -c '[0-9] '
     -c除了什么不删,其他的都要删   除了[0-9]空格不删,其它的都会删除

        8. 去重字符串"abcccd   dedd"

     echo "abcccd   dedd" | tr -s '[[:alpha:]] '  去重[a-z] 及空格