Linux常用基本命令:tr-替换或者删除字符

tr命令

作用:从标准输入中替换,缩减或者删除字符,并将结果输出到标准输出

格式:tr [option] [set1] [set2]

tr [选项] [字符1] [字符2]

把y替换m, o替换e,并不仅仅是yo替换me

ghostwu@dev:~/linux/tr$ cat ghostwu.txt 
hello,my name is ghostwu,
my qq is 359173352@qq.com
my blog is http://www.cnblogs.com/ghostwu
nice to meet you,
feel free to contact me.
ghostwu@dev:~/linux/tr$ tr 'me' 'yo' < ghostwu.txt 
hollo,yy nayo is ghostwu,
yy qq is 359173352@qq.coy
yy blog is http://www.cnblogs.coy/ghostwu
nico to yoot you,
fool froo to contact yo.

小写字母变成大写

ghostwu@dev:~/linux/tr$ tr '[a-z]' '[A-Z]' < ghostwu.txt 
HELLO,MY NAME IS GHOSTWU,
MY QQ IS 359173352@QQ.COM
MY BLOG IS HTTP://WWW.CNBLOGS.COM/GHOSTWU
NICE TO MEET YOU,
FEEL FREE TO CONTACT ME.

-d: 删除.     删除数字

ghostwu@dev:~/linux/tr$ tr -d '[0-9]' < ghostwu.txt 
hello,my name is ghostwu,
my qq is @qq.com
my blog is http://www.cnblogs.com/ghostwu
nice to meet you,
feel free to contact me.

删除a, b, c中出现的任意一个字母

ghostwu@dev:~/linux/tr$ tr -d ['a-c'] < ghostwu.txt 
hello,my nme is ghostwu,
my qq is 359173352@qq.om
my log is http://www.nlogs.om/ghostwu
nie to meet you,
feel free to ontt me.

删除文件中的换行符

ghostwu@dev:~/linux/tr$ tr -d '\n' < ghostwu.txt 
hello,my name is ghostwu,my qq is 359173352@qq.commy blog is http://www.cnblogs.com/ghostwunice to meet you,feel free to contact me.

-s:保留连续字符的第一个,其他的删除。 压缩连续字符

ghostwu@dev:~/linux/tr$ echo 'gggghhostwwwu' | tr -s ghostwu
ghostwu

-c:取反。 把所有的非数字 都变成 #

ghostwu@dev:~/linux/tr$ tr '[0-9]' '#' < ghostwu.txt 
hello,my name is ghostwu,
my qq is #########@qq.com
my blog is http://www.cnblogs.com/ghostwu
nice to meet you,
feel free to contact me.
ghostwu@dev:~/linux/tr$ tr -c '[0-9]' '#' < ghostwu.txt 
###################################359173352#############################################################################################ghostwu@dev:~/linux/tr$

 

posted @ 2018-05-20 22:29 ghostwu 阅读(...) 评论(...) 编辑 收藏
Copyright ©2017 ghostwu