标签:tr 简单使用
tr 替换
1.用来计算求和
[root@room4pc09 桌面]# seq 1 100 |echo "$[`tr "\n" "+"` 0]"
5050
4pc09 桌面]# seq 1 2 100 |echo "$[`tr "\n" "+"` 0]"
2500
[root@room4pc09 桌面]# seq 0 2 100 |echo "$[`tr "\n" "+"` 0]"
2550
2.用来求乘
[root@room4pc09 桌面]# seq 1 3 |echo "$[`tr "\n" "*"` 1]"
6
[root@room4pc09 桌面]# seq 1 5 |echo "$[`tr "\n" "*"` 1]"
120
[root@room4pc09 桌面]# seq 1 4 |echo "$[`tr "\n" "*"` 1]"
24
___________
[root@room4pc09 桌面]# seq 1 2 10 |echo "`tr "\n" "," `"
1,3,5,7,9,
[root@room4pc09 桌面]# seq 1 2 10 |echo "`tr "\n" "+" `"
1+3+5+7+9+
[root@room4pc09 桌面]# seq 1 2 10 |echo "`tr "\n" "+" `0"
1+3+5+7+9+0
[root@room4pc09 桌面]# seq 1 2 10 |echo "$[`tr "\n" "+" `0]"
25
____________
大小写替换
[root@room4pc09 桌面]# cat 1.sh
1.5 1.55 2.1
1.5 2.2 1.54
[root@room4pc09 桌面]# tr 1 2 <1.sh
2.5 2.55 2.2
2.5 2.2 2.54
[root@room4pc09 桌面]# cat 1.TXT
abcd12ef
[root@room4pc09 桌面]# tr [a-z] [A-Z] <1.TXT
ABCD12EF
————————————————
-s 选项是把多个连续相同字符 只保留一个
-d 是删除删除字符的
替换空格删除空格
[root@room4pc09 桌面]# !c
cat 1.ttx
how are you !
你 好!
[root@room4pc09 桌面]# tr -s " " " " <1.ttx
how are you !
你 好!
[root@room4pc09 桌面]# tr -d " " <1.ttx
howareyou!
你好!
[root@room4pc09 桌面]# cat 2.txt
sdadsd
ssddffs
sdafsafsf
[root@room4pc09 桌面]# tr -s "s" "s" <2.txt
sdadsd
sddffs
sdafsafsf
[root@room4pc09 桌面]# tr -d "f" <2.txt
sdadsd
ssdds
sdasas
[root@room4pc09 桌面]# cat 2.txt
sd ads d
ssddf fs
sdafsaf sf
[root@room4pc09 桌面]# tr -s " " " " <2.txt
sd ads d
ssddf fs
sdafsaf sf
应用例子
(1)去除oops.txt里面的重复的小写字符
tr -s "[a-z]"<oops.txt >relt.txt
(2)删除空行
tr -s "[\012]" < plan.txt 或 tr -s ["\n"] < plan.txt
(3)有时需要删除文件中的^M,并代之以换行
tr -s "[\015]" "[\n]" < file 或 tr -s "[\r]" "[\n]" < file
本文出自 “12336621” 博客,请务必保留此出处http://12346621.blog.51cto.com/12336621/1912558
标签:tr 简单使用
原文地址:http://12346621.blog.51cto.com/12336621/1912558