tr tracslate 的簡寫
例1 替換字元,把小寫r替換成大寫的R
shuohailhl@shuohailhl-PC /cygdrive/d$ cat pass.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinshuohailhl@shuohailhl-PC /cygdrive/d$ tr 'r' 'R' < ./pass.txtRoot:x:0:0:Root:/Root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/vaR/adm:/sbin/nologin
例2 把小寫全換成大寫
$ tr '[a-z]' '[A-Z]' < ./pass.txtROOT:X:0:0:ROOT:/ROOT:/BIN/BASHBIN:X:1:1:BIN:/BIN:/SBIN/NOLOGINDAEMON:X:2:2:DAEMON:/SBIN:/SBIN/NOLOGINADM:X:3:4:ADM:/VAR/ADM:/SBIN/NOLOGIN
例 3
shuohailhl@shuohailhl-PC /cygdrive/d$ a=`echo 123456789 | tr '1-9' '[A*5]'BCDE` #前5個字元替換成Ashuohailhl@shuohailhl-PC /cygdrive/d$ echo $aAAAAABCDEshuohailhl@shuohailhl-PC /cygdrive/d$ a=`echo 123456789 | tr '1-9' '[A*]'BCDE` #不指定A的替換的數量,shuohailhl@shuohailhl-PC /cygdrive/d$ echo $aAAAAABCDEshuohailhl@shuohailhl-PC /cygdrive/d$ a=`echo 123456789 | tr '1-9' '[A*6]'BCDE` #指定替換個數為6,則最後一個E就沒了shuohailhl@shuohailhl-PC /cygdrive/d$ echo $aAAAAAABCD