首页 > 其他 > 详细

3.20 tr:替换或删除字符

时间:2019-05-19 19:45:56      阅读:92      评论:0      收藏:0      [点我收藏+]

tr命令

  从标准输入中替换、缩减或删除字符,并将结果写到标准输出。
tr [option] [SET1]  [SET2]
tr [选项]   [字符1]  [字符2]
 
-d    删除字符
-s    保留连续字符的第一个字符,删除其他字符
-c    使用第一个字符率(set1)的补集,取反
 
[root@cs6 ~]# cat lewen.txt
I am lewen student!
I love linux.
 
I like badminton ball,billiard ball and chinese chess!
my blog is http://lewen.blog.51cto.com
our site is http://www.wenyule.top
my qq num is 846009315
 
not 8460009315
my god,i am not oldboy, but lewen!

将文件中出现的“abc”替换为“xyz”

[root@cs6 ~]# tr abc xyz <lewen.txt    #<== tr命令接文件比较特殊,需要输入重定向符号“<”。
I xm lewen student!
I love linux.
 
I like yxdminton yxll,yillixrd yxll xnd zhinese zhess!
my ylog is http://lewen.ylog.51zto.zom
our site is http://www.wenyule.top
my qq num is 846009315
 
not 8460009315
my god,i xm not oldyoy, yut lewen!
 
    凡是在文本中出现的“a”均应转换成“x”,“b”均应转换成“y”,“c”均应转换成“z”,而不是仅仅将字符串“abc”替换为字符串“xyz”。
 
使用tr命令“统一”字母大小写
[root@cs6 ~]# tr [a-z] [A-Z] <lewen.txt
I AM LEWEN STUDENT!
I LOVE LINUX.
 
I LIKE BADMINTON BALL,BILLIARD BALL AND CHINESE CHESS!
MY BLOG IS HTTP://LEWEN.BLOG.51CTO.COM
OUR SITE IS HTTP://WWW.WENYULE.TOP
MY QQ NUM IS 846009315
 
NOT 8460009315
MY GOD,I AM NOT OLDBOY, BUT LEWEN!
将数字0-9替换为a-j
[root@cs6 ~]# tr [0-9] [a-j] <lewen.txt
I am lewen student!
I love linux.
 
I like badminton ball,billiard ball and chinese chess!
my blog is http://lewen.blog.fbcto.com
our site is http://www.wenyule.top
my qq num is iegaajdbf
 
not iegaaajdbf
my god,i am not oldboy, but lewen!

删除文件中出现的lewen中的每个字符

[root@cs6 ~]# tr -d lewen <lewen.txt
I am  studt!
I ov iux.
 
I ik badmito ba,biiard ba ad chis chss!
my bog is http://.bog.51cto.com
our sit is http://.yu.top
my qq um is 846009315
 
ot 8460009315
my god,i am ot odboy, but !
删除文件中出现的换行"\n",制表,"\t" 字符
[root@cs6 ~]# tr -d \n\t < lewen.txt
I am lewen student!I love linux.I like badminton ball,billiard ball and chinese chess!my blog is http://lewen.blog.51cto.comour site is http://www.wenyule.topmy qq num is 846009315not 8460009315my god,i am not oldboy, but lewen![root@cs6 ~]#

删除连续字符(-s)的例子

[root@cs6 ~]# echo llllleeeewwwweennn|tr -s lewen    #<==使用-s参数将连续的字符压缩成一个。
lewen

取反功能(-c)的例子

[root@cs6 ~]# tr 0-9 * <lewen.txt
I am lewen student!
I love linux.
 
I like badminton ball,billiard ball and chinese chess!
my blog is http://lewen.blog.**cto.com
our site is http://www.wenyule.top
my qq num is *********
 
not **********
my god,i am not oldboy, but lewen!
 
 
[root@cs6 ~]# tr -c 0-9 * <lewen.txt    #<==使用参数-c,除了数字,其他的字符包括换行符都会替换为************************************************************************************************************************51********************************************************846009315******8460009315************************************[root@cs6 ~]#

 

 
 
 

3.20 tr:替换或删除字符

原文:https://www.cnblogs.com/fadewalk/p/10890348.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!