Tr

tr (translate)12

功能作用(Function)

转化、压缩和/或删除标准输入中的字符, 将其写入标准输出。

Translate, squeeze, and/or delete characters from standard input, writing to standard output.

语法(Synopsis)

1
tr [OPTION]... SET1 [SET2]

选项(Option)

  • -c, -C, --complement: 使用SET1的补集(use the complement of SET1)
  • -d, --delete: 删除SET1中的字符, 不转化(delete characters in SET1, do not translate)
  • -s, --squeeze-repeats: 将最后指定的SET中列出的重复字符的每个序列替换为该字符的单次出现(replace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character)
  • -t, --truncate-set1: 截断SET1到SET2的长度(first truncate SET1 to length of SET2)

描述(Description)

SET通常为字符串。其中转义字符3有:

  • \NNN: 三位八进制数值(character with octal value NNN (1 to 3 octal digits))
  • \\: 反斜线(backslash)
  • \a: 响铃符(audible BEL)4
  • \b: 退格符(backspace)
  • \f: 换页符(form feed)
  • \n: 换行符(new line)
  • \r: 回车符(return)
  • \t: 水平制表符(horizontal tab)
  • \v: 垂直制表符(vertical tab)

SET可以由以下部分组成:

  • CHAR1-CHAR2: 从CHAR1到CHAR2的所有字符按升序排列
  • [CHAR*]: 在SET2中, SET1的长度个CHAR副本
  • [CHAR*REPEAT]: REPEAT个CHAR副本, 如果REPEAT以0开始, 则为八进制数
  • [:alnum:]: 所有字母和数字
  • [:alpha:]: 所有字母
  • [:blank:]: 所有水平空白
  • [:cntrl:]: 所有控制字符5
  • [:digit:]: 所有数字
  • [:graph:]: 所有可打印字符, 不包括空格
  • [:lower:]: 所有小写字母
  • [:print:]: 所有可打印字符, 包括空格
  • [:punct:]: 所有标点符号
  • [:space:]: 所有水平或垂直空白
  • [:upper:]: 所有大写字母
  • [:xdigit:]: 所有十六进制数字
  • [=CHAR=]: 所有等价于CHAR的字符

如果没有给出-dSET1SET2都出现, 则进行转化(translation)。-t只能在转化时时使用。

SET2的长度小于SET1的长度, 以SET2的最后一个字符对SET2进行扩展并扩展到与SET1长度相同。

SET2的长度大于SET1的长度, SET2中多余的字符将被忽略。

只有[:lower:][:upper:]保证按升序展开;在含有SET2的转化中, 它们只能成对使用来进行大小写转换。-s使用最后一个指定的SET, 并用于转化(translation)或删除(deletion)。

示例(Example)

小写转大写

1
echo "hello\t10023 world 456" | tr "a-z" "A-Z"
1
HELLO	10023 WORLD 456

"a-z", "A-Z", 都是SET, 分别表示所有的小写字母和所有的大写字母。

制表符转空格

1
echo "hello\t10023 world 456" | tr "\t" " "
1
hello 10023 world 456

删除数字

1
echo "hello\t10023 world 456" | tr -d "0-9"
1
hello	 world

只保留数字

1
echo "hello\t10023 world 456" | tr -cd "0-9"
1
10023456

压缩字符9p

1
echo "hello\t199923 world app 456" | tr -s "9p"
1
hello	1923 world ap 456

截断SET1

1
echo "abcd" | tr -t "abcd" "123"
1
123d
1
echo "abcd" | tr "abcd" "123"
1
1233

man tr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
TR(1)                            User Commands                           TR(1)



NAME
tr - translate or delete characters

SYNOPSIS
tr [OPTION]... SET1 [SET2]

DESCRIPTION
Translate, squeeze, and/or delete characters from standard input, writ-
ing to standard output.

-c, -C, --complement
use the complement of SET1

-d, --delete
delete characters in SET1, do not translate

-s, --squeeze-repeats
replace each sequence of a repeated character that is listed in
the last specified SET, with a single occurrence of that charac-
ter

-t, --truncate-set1
first truncate SET1 to length of SET2

--help display this help and exit

--version
output version information and exit

SETs are specified as strings of characters. Most represent them-
selves. Interpreted sequences are:

\NNN character with octal value NNN (1 to 3 octal digits)

\\ backslash

\a audible BEL

\b backspace

\f form feed

\n new line

\r return

\t horizontal tab

\v vertical tab

CHAR1-CHAR2
all characters from CHAR1 to CHAR2 in ascending order

[CHAR*]
in SET2, copies of CHAR until length of SET1

[CHAR*REPEAT]
REPEAT copies of CHAR, REPEAT octal if starting with 0

[:alnum:]
all letters and digits

[:alpha:]
all letters

[:blank:]
all horizontal whitespace

[:cntrl:]
all control characters

[:digit:]
all digits

[:graph:]
all printable characters, not including space

[:lower:]
all lower case letters

[:print:]
all printable characters, including space

[:punct:]
all punctuation characters

[:space:]
all horizontal or vertical whitespace

[:upper:]
all upper case letters

[:xdigit:]
all hexadecimal digits

[=CHAR=]
all characters which are equivalent to CHAR

Translation occurs if -d is not given and both SET1 and SET2 appear.
-t may be used only when translating. SET2 is extended to length of
SET1 by repeating its last character as necessary. Excess characters
of SET2 are ignored. Only [:lower:] and [:upper:] are guaranteed to
expand in ascending order; used in SET2 while translating, they may
only be used in pairs to specify case conversion. -s uses the last
specified SET, and occurs after translation or deletion.

AUTHOR
Written by Jim Meyering.

REPORTING BUGS
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>

COPYRIGHT
Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU
GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
Full documentation <https://www.gnu.org/software/coreutils/tr>
or available locally via: info '(coreutils) tr invocation'



GNU coreutils 8.31 March 2019 TR(1)

Reference

1. “tr命令_Linux tr 命令用法详解:将字符进行替换压缩和删除.” [Online]. Available: https://man.linuxde.net/tr. [Accessed: 12-Feb-2020]
2. “Tr (Unix),” 维基百科, 自由的百科全书. 21-Dec-2019 [Online]. Available: https://zh.wikipedia.org/w/index.php?title=Tr_(Unix)&oldid=57354145. [Accessed: 12-Feb-2020]
3. “转义字符_百度百科.” [Online]. Available: https://baike.baidu.com/item/%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6/86397?fromtitle=%E8%BD%AC%E4%B9%89%E7%AC%A6&fromid=6151115. [Accessed: 12-Feb-2020]
4. “Bell character,” Wikipedia. 14-Sep-2019 [Online]. Available: https://en.wikipedia.org/w/index.php?title=Bell_character&oldid=915701003. [Accessed: 12-Feb-2020]
5. “控制字符,” 维基百科,自由的百科全书. 31-Jan-2018 [Online]. Available: https://zh.wikipedia.org/w/index.php?title=%E6%8E%A7%E5%88%B6%E5%AD%97%E7%AC%A6&oldid=48096464. [Accessed: 12-Feb-2020]