实例:
、将文件file中出现的"abc"替换为"xyz"
# cat file | tr "abc" "xyz" > new_file
【注意】这里,凡是在file中出现的"a"字母,都替换成"x"字母,"b"字母替换为"y"字母,"c"字母替换为"z"字母。而不是将字符串"abc"替换为字符串"xyz"。
2、使用tr命令“统一”字母大小写
(小写 --> 大写)
# cat file | tr [a-z] [A-Z] > new_file
# cat file | tr [a-z] [A-Z] > new_file
(大写 --> 小写)
# cat file | tr [A-Z] [a-z] > new_file
# cat file | tr [A-Z] [a-z] > new_file
3、把文件中的数字0-9替换为a-j
# cat file | tr [0-9] [a-j] > new_file
4、删除文件file中出现的"Snail"字符
# cat file | tr -d "Snail" > new_file
【注意】这里,凡是在file文件中出现的'S','n','a','i','l'字符都会被删除!而不是紧紧删除出现的"Snail”字符串。
5、删除文件file中出现的换行'
'、制表' '字符
# cat file | tr -d "
" > new_file
不可见字符都得用转义字符来表示的,这个都是统一的。
6、删除“连续着的”重复字母,只保留第一个
# cat file | tr -s [a-zA-Z] > new_file
7、删除空行
# cat file | tr -s "
" > new_file
8、删除Windows文件“造成”的'^M'字符
# cat file | tr -d "
" > new_file
或者
# cat file | tr -s " " " " > new_file
或者
# cat file | tr -s " " " " > new_file
【注意】这里-s后面是两个参数"
"和"
",用后者替换前者
9、用空格符 40替换制表符 11
# cat file | tr -s " 11" " 40" > new_file
10、把路径变量中的冒号":",替换成换行符"
"
# echo $PATH | tr -s ":" "
"