您的位置:首页 > 运维架构 > Linux

linux tr命令对来自标准输入的字符进行转换、删除及压缩

2012-06-03 09:21 811 查看
tr命令对来自标准输入的字符进行转换、删除及压缩,是个转换命令
tr [options] set1 set2 就是把set1集合中的内容换成set2集合中的内容
1将大写字母变为小写字母

[root@localhost test]# echo "HELLO LUFUBO NICE TO MEET YOU" |tr 'A-Z' 'a-z'

hello lufubo nice to meet you

2将数字加密与解密:集合映射

[root@localhost test]# echo 12345 | tr '0-9' '987654321'

87654

[root@localhost test]# echo 87654 | tr '9876543210' '0-9'

12345

3 -d删除字符

[root@localhost test]# echo "hello 123 world 456" | tr -d '0-9'

hello world

4 -c 集合的补集
从输入文本中将不在补集中的所有字符全部删除

[root@localhost test]# echo "hello 123 world 456" | tr -d -c '0-9'

123456

5 -s 压缩重复字符

[root@localhost test]# echo "hello lufubo" | tr -s ' '

hello lufubo

6 将文件中的进行相加

[root@localhost test]# cat sum.txt

1

2

3

4

5

[root@localhost test]# cat sum.txt | echo $[ $(tr '\n' '+') 0 ] #0是为了最后个‘/n’替换成+后再加个0的意思,这个可以成将文本里面的所有数字相加哟。~^~

15

7 字符类
tr可以像使用集合一样使用各种不同的字符类,这些字符如下:
alnum:字母和数字
alpha:字母
cntrl:控制字符
digit:数字
graph:图形字符
lower:小写字母
print:可打印字符
punct:标点符号
space:空白字符
upper:大写字母
xdigit:十六进制字符

语法: tr [:class:] [:class:]
例如

[root@localhost test]# echo lufubo | tr '[:lower:]' '[:upper:]'

LUFUBO
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: