tr命令

1.tr命令

tr命令不能从文件中读取数据,只能从标准输出中获取数据,结果写到输出设备。

tr命令可以批量转换或删除指定的字符。


2.tr命令的使用方法

语法:

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


选项:

-d:删除指定的字符集1的东西

-s:压缩字符集1中字符。一排重复的字符会压缩成一个

-t:字符集2 替换 字符集1,不加选项同结果。

-c:保留字符集1的字符,其他的字符用字符集2替换。


字符集:

[a-z][A-Z][0-9]:很好理解的范围。

[xx*n]:表示xx出现n次

和所有元字符


3.实例

3.1去字符的重复 -s

s选项可以压缩文本中连续出现的字符只保留一个。

[root@ams d6]# echo "hhhhi,woooorllld" > 2.txt

[root@ams d6]# tr -s [a-z]  < 2.txt

hi,world


去除空行

[root@ams d6]# echo -e "aa\n\n\n\n\naa" > 2.txt  

[root@ams d6]# tr -s "\n" < 2.txt  

aa

aa


3.2 大小写转换 -t

用第二个字符集 替换 第一个字符集。

[root@ams d6]# ls | tr 'a-z' 'A-Z'

1.TXT

2.TXT


3.3 删除指定字符 -d

单个字符直接引用起来就可以,多个字符需要用中括号括起来。

[root@ams d6]# date | tr -d "[0-9] [:]"

WedSepCST


猜你喜欢

转载自blog.51cto.com/11060853/2107289
今日推荐