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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值