tr用来从标准输入中通过替换或删除操作进行字符转换。tr主要用于删除文件中控制字符或进行字符转换。
tr中特定控制字符的不同表达方式
速记符 含义 八进制方式
\a Ctrl-G铃声 \007
\b Ctrl-H退格符 \010
\f Ctrl-L 走行换页 \014
\n Ctrl-J 新行 \012
\r Ctrl-M 回车 \015
\t Ctrl-I tab键 \011
\v Ctrl-X \030
$cat oops.txt
And the cowwwws went homeeeeeee
Or did they
删除重复字母
$tr -s "[a-z]"< oops.txt
And the cows went home
Or did they
设有plane.txt
$cat plane.txt
987932 Spitfire
190992 Lancaster
238991 Typhoon
用-s删除空行
$tr -s "[\012]" < plane.txt或 $tr -s "[\n]" < plane.txt
987932 Spitfire
190992 Lancaster
238991 Typhoon
大写到小写
$echo "May Day, May Day, Going Down.." | tr "[a-z]" "[A-Z]"
或$echo "May Day, May Day, Going Down.." | tr "[:lower:]" "[:upper:]"
$ cat diary.txt
monday 10:50
Tuesday 15:30
wednesday 15:30
thurday 10:30
Friday 09:20
删除后面的时刻,可用
$ tr -cs "[a-z][A-Z]" "[\012*]" < diary.txt
monday
Tuesday
wednesday
thurday
Friday