| 对文本的简单处理有时候可能使用tr更高效,看看它的一些常规用法吧! 对于tr的输入通过重定向或管道都可以,除第一例子之外均使用管道表示,重定义的与例1类似。
去除重复的字符
tr -s "[a-z]" < example.txt
cat example.txt | tr -s "[a-z]"
删除空行
cat example.txt | tr -s "[\n]"
cat example.txt | tr -s "[2]"
小写到大写
cat example.txt | tr "[a-z]" "[A-Z]"
cat example.txt | tr "[:lower:]" "[:upper:]"
大写到小写
与上例相反
Dos到Unix
cat example.txt | tr -s "[52]" "[2*]"
cat example.txt | tr -s "[\r2]" "[\n*]"
删除所有的tab代之以空格
cat example.txt | tr -s "[1]" "[0*]"
cat example.txt | tr -s "[\t]" "[0*]"
去除passwd的:以tab键代替增加可读性
cat /etc/passwd | tr -s "[:]" "[1]"
cat /etc/passwd | tr -s "[:]" "[\t]" |