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
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
相关文章推荐
- tr命令_Linux tr 命令用法详解:将字符进行替换压缩和删除
- tr对标准输入字符替换压缩和删除
- Shell学习:字符转换、删除及压缩工具 tr命令
- Linux下统计文件行数 语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数、字数、行数。如果没有给出文件名,则从标准输入读取。wc同时也给出所有 指定文件的总统计数。字是由空格字符
- linux命令之字符转换命令:tr, col, join, paste
- linux中字符转换命令-tr的用法
- 【Linux】字符转换命令tr
- tr命令详解 Linux字符转换
- Linux下五个字符转换命令:tr,col,join,paste,expand
- linux新建用户后shell无法自动补全命令,Backspace无法删除字符,无法翻看输入纪录
- linux中的tr命令---字符的替换与删除
- linux中字符转换命令-tr的用法
- 【tr】字符处理命令,用于实现转换或者删除字符
- Linux命令学习_tr_转换字符
- linux中字符转换命令-tr的用法
- logrotate命令_Linux logrotate 命令用法详解:统日志进行轮转、压缩和删除
- 【转载】 Linux命令 -- tr 转换字符
- linux---tr,col,join,paste,expand 字符转换命令
- linux tr命令参数及用法详解---删除文件中控制字符或进行字符转换
- Linux系统中tr命令删除和替换文本字符的基本用法