tr 命令可以对标准输入字符进行转移、压缩、删除,写到标准输出。作用类似于sed ,但不能从文件中读取数据。
1.基本语法
man tr 如下:
语法:
SYNOPSIS
tr [OPTION]... SET1 [SET2]
其选项有如下:
说明:
-c ,-C, --complement 用集合1中的字符串替换
-d,---delete 删除集合1中的字符串,而不进行转移
-s, --squeeze-repeats 把集合1中的每个重复的字符串替换为单个字符
-t,--truncate-set1 删除第一字符集比第二字符集多出的字符
可用字符集合有如下:
\NNN 八进制值的字符 NNN (1 to 3 为八进制值的字符)
\\ 反斜杠
\a 响铃
\b 退格
\f 换页
\n 换行
\r 回车
\t 水平制表符
\v垂直制表符
all characters from char1 to char2 in ascengding order 从CHAR1 到 CHAR2 的所有字符按照升序排序
[CHAR*] in SET2, copies of CHAR until length of SET1
[CHAR*REPEAT] REPEAT copies of CHAR, REPEAT octal if starting with 0
[:alnum:] 所有的字母和数字
[:alpha:] 所有字母
[:blank:] 所有水平空白
[:cntrl:] 所有控制字符
[:digit:] 所有数字
[:graph:] 所有可打印的字符,不包括白符
[:lower:] 所有小写字母
[:print:] 所有可打印的字符,不包括白符
[:punct:] 所有标点符号
[:space:] 所有水平或垂直空白
[:upper:] 所有大写字母
[:xdigit:] 所有十六进制数字
[=CHAR=] 所有与CHAR等价的字符
2.实例
1.大小写转换
zhang@zhang-virtual-machine:~/peotry$ ls
Shakespeare.peotry test.txt
zhang@zhang-virtual-machine:~/peotry$ cat Shakespeare.peotry
The lunatic, the lover and the poet are of imagination all compact.
zhang@zhang-virtual-machine:~/peotry$ cat Shakespeare.peotry | tr 'a-z' 'A-Z'
THE LUNATIC, THE LOVER AND THE POET ARE OF IMAGINATION ALL COMPACT.
zhang@zhang-virtual-machine:~/peotry$ cat Shakespeare.peotry | tr [a-z] [A-Z]
THE LUNATIC, THE LOVER AND THE POET ARE OF IMAGINATION ALL COMPACT.
zhang@zhang-virtual-machine:~/peotry$ cat Shakespeare.peotry | tr [:lower:] [:upper:]
THE LUNATIC, THE LOVER AND THE POET ARE OF IMAGINATION ALL COMPACT.
zhang@zhang-virtual-machine:~/peotry$ cat Shakespeare.peotry | tr [:lower:] [:upper:] >> upper.txt
zhang@zhang-virtual-machine:~/peotry$ cat upper.txt
THE LUNATIC, THE LOVER AND THE POET ARE OF IMAGINATION ALL COMPACT.
zhang@zhang-virtual-machine:~/peotry$ cat upper.txt | tr [:upper:] [:lower:]
the lunatic, the lover and the poet are of imagination all compact.
zhang@zhang-virtual-machine:~/peotry$
可以看到tr 命令支持多种写法的
2.字符串替换
zhang@zhang-virtual-machine:~/peotry$ ls
Shakespeare.peotry test.txt upper.txt
zhang@zhang-virtual-machine:~/peotry$ cat Shakespeare.peotry
The lunatic, the lover and the poet are of imagination all compact.
zhang@zhang-virtual-machine:~/peotry$ cat Shakespeare.peotry | tr 'lo' 'LO'
The Lunatic, the LOver and the pOet are Of imaginatiOn aLL cOmpact.
zhang@zhang-virtual-machine:~/peotry$
将数字替换成字母
zhang@zhang-virtual-machine:~/peotry$ cat test.txt
港泰 李坚焕 搜于特 净色单位衣
183CM x 250g/㎡ 20s/1 棉
20s/1 棉
白色01 - 4,552.00 ping_test_1_2 120.00 330.00
zhang@zhang-virtual-machine:~/peotry$ cat test.txt | tr -d '\n'
港泰 李坚焕 搜于特 净色单位衣183CM x 250g/㎡ 20s/1 棉20s/1 棉白色01 - 4,552.00 ping_test_1_2 120.00 330.00zhang@zhang-virtual-machine:~/peotry$
zhang@zhang-virtual-machine:~/peotry$ cat test.txt | tr -d '\n' | tr '0-9' 'A-Z'
港泰 李坚焕 搜于特 净色单位衣BIDCM x CFAg/㎡ CAs/B 棉CAs/B 棉白色AB - E,FFC.AA ping_test_B_C BCA.AA DDA.AAzhang@zhang-virtual-machine:~/peotry$
3.删除空行
zhang@zhang-virtual-machine:~/peotry$ ls
Shakespeare.peotry test.txt upper.txt
zhang@zhang-virtual-machine:~/peotry$ cat test.txt
港泰 李坚焕 搜于特 净色单位衣
183CM x 250g/㎡ 20s/1 棉
20s/1 棉
白色01 - 4,552.00 ping_test_1_2 120.00 330.00
zhang@zhang-virtual-machine:~/peotry$ cat test.txt | tr -d '\n'
港泰 李坚焕 搜于特 净色单位衣183CM x 250g/㎡ 20s/1 棉20s/1 棉白色01 - 4,552.00 ping_test_1_2 120.00 330.00zhang@zhang-virtual-machine:~/peotry$
删除数字和特殊符号
zhang@zhang-virtual-machine:~/peotry$ cat test.txt
港泰 李坚焕 搜于特 净色单位衣
183CM x 250g/㎡ 20s/1 棉
20s/1 棉
白色01 - 4,552.00 ping_test_1_2 120.00 330.00
zhang@zhang-virtual-machine:~/peotry$ cat test.txt | tr -d '0-9'
港泰 李坚焕 搜于特 净色单位衣
CM x g/㎡ s/ 棉
s/ 棉
白色 - ,. ping_test__ . .
zhang@zhang-virtual-machine:~/peotry$ cat test.txt | tr -d '0-9' | tr -d [:punct:]
港泰 李坚焕 搜于特 净色单位衣
CM x g㎡ s 棉
s 棉
白色 pingtest
zhang@zhang-virtual-machine:~/peotry$
4.进行计算
可以结合bc 等命令进行简单的运算
zhang@zhang-virtual-machine:~/peotry$ seq 1 10
1
2
3
4
5
6
7
8
9
10
zhang@zhang-virtual-machine:~/peotry$ seq 1 10 | tr '\n' '+'
1+2+3+4+5+6+7+8+9+10+zhang@zhang-virtual-machine:~/peotry$
zhang@zhang-virtual-machine:~/peotry$
zhang@zhang-virtual-machine:~/peotry$ echo `seq 1 10 | tr '\n' '+' ` 0
1+2+3+4+5+6+7+8+9+10+ 0
zhang@zhang-virtual-machine:~/peotry$ echo `seq 1 10 | tr '\n' '+' ` 0 | bc
55
zhang@zhang-virtual-machine:~/peotry$ echo $((`seq 1 10 | tr '\n' '+' ` 0 ))
55
zhang@zhang-virtual-machine:~/peotry$