目录
-
用途
tr - translate or delete characters
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
转换和删除字符
对标准输入的字符进行转换,压缩和删除,然后写入到标准输出
-
语法格式
tr [OPTION]... SET1 [SET2]
常用格式:
tr SET1 SET2
用SET2中字符替换SET1中的字符
tr -d SET1
删除SET1中的字符
tr -s SET1
压缩SET1中的字符
OPTION:
- -c, -C, --complement:反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换
- -d, --delete:删除指令字符
- -s, --squeeze-repeats:缩减连续重复的字符成指定的单个字符
- -t, --truncate-set1:削减 SET1 指定范围,使之与 SET2 设定长度相等
- --help:显示程序用法信息
- --version:显示程序本身的版本信息
字符集合的范围:
- \NNN 八进制值的字符 NNN (1 to 3 为八进制值的字符)
- \\ 反斜杠
- \a Ctrl-G 铃声
- \b Ctrl-H 退格符
- \f Ctrl-L 走行换页
- \n Ctrl-J 新行
- \r Ctrl-M 回车
- \t Ctrl-I tab键
- \v Ctrl-X 水平制表符
- CHAR1-CHAR2 :字符范围从 CHAR1 到 CHAR2 的指定,范围的指定以 ASCII 码的次序为基础,只能由小到大,不能由大到小。
- [CHAR*] :这是 SET2 专用的设定,功能是重复指定的字符到与 SET1 相同长度为止
- [CHAR*REPEAT] :这也是 SET2 专用的设定,功能是重复指定的字符到设定的 REPEAT 次数为止(REPEAT 的数字采 8 进位制计算,以 0 为开始)
- [:alnum:] :所有字母字符与数字
- [:alpha:] :所有字母字符
- [:blank:] :所有水平空格
- [:cntrl:] :所有控制字符
- [:digit:] :所有数字
- [:graph:] :所有可打印的字符(不包含空格符)
- [:lower:] :所有小写字母
- [:print:] :所有可打印的字符(包含空格符)
- [:punct:] :所有标点字符
- [:space:] :所有水平与垂直空格符
- [:upper:] :所有大写字母
- [:xdigit:] :所有 16 进位制的数字
- [=CHAR=] :所有符合指定的字符(等号里的 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.
-
实例
1. -c, -C, --complement
use the complement of SET1 使用SET1的补足部分即反选,不常用
renhl@verygood:~/usb$ echo "123abc" | tr -d 'a-z' #删除123abc中的a-z的小写字符
123
renhl@verygood:~/usb$ echo "123abc" | tr -d -c 'a-z' #删除123abc中的除了a-z的其余字符,删除了123和回车
abcrenhl@verygood:~/usb$
2. -d, --delete
delete characters in SET1, do not translate
删除SET1中的字符,不进行转换
renhl@verygood:~/usb$ echo "123abc" | tr -d 'a-z' #删除123abc中的a-z的字符,a-z表示a到z的小写字符
123
renhl@verygood:~/usb$ echo "123abc" | tr -d "a-z" #SET用单引号或者双引号都可以
123
3. -s, --squeeze-repeats
replace each sequence of a repeated character that is listed in the last specified SET, with a single occurrence of that character
用一个字符替换重复字符序列,这个字符在最后一个SET中指定。
renhl@verygood:~/usb$ echo "aaa123bbb123ccc" | tr -s 'abc' #对"aaa123bbb123ccc" 中的abc重复字符进行压缩
a123b123c
renhl@verygood:~/usb$ echo "aaa123bbb123ccc" | tr -s 'ab' #对"aaa123bbb123ccc" 中的ab重复字符进行压缩
a123b123ccc
renhl@verygood:~/usb$ echo "aaa123bbb123ccc" | tr '123' 'abc' #对"aaa123bbb123ccc" 中的123替换成abc
aaaabcbbbabcccc
renhl@verygood:~/usb$ echo "aaa123bbb123ccc" | tr -s '123' 'abc' #对"aaa123bbb123ccc"替换后的结果进行abc压缩,所以-s指定的最后一个SET
abcbabc
4. -t, --truncate-set1
first truncate SET1 to length of SET2
转换是SET1中一个字符对应SET2中的一个字符,一对一转换。比如 tr 'abc' '123' a->1 b->2 c->3.
当出现SET1中字符数大于SET2中字符数时,默认行为重复SET2的最后一个字符。
-t 参数作用截断SET1的字符数和SET2的相等,也就是SET1中多余字符不进行转换。
SET1 == SET2 一对一转换
SET1 > SET2 默认重复SET2的最后字符使得字符个数相等,一对一转换;加-t参数SET1多余的不进行转换
SET1 < SET2 SET2中多余字符不参与转换
renhl@verygood:~/usb$ echo "123" | tr '123' 'abc' #SET1和SET2字符数相等,一对一替换
abc
renhl@verygood:~/usb$ echo "123" | tr '123' 'ab' #SET1字符数大于SET2时,默认重复SET2最后一个字符b,达到和SET1的个数相等
abb
renhl@verygood:~/usb$ echo "123" | tr -t '123' 'ab' #SET1字符数大于SET2时,-t的作用截断SET1的个数3和SET2个数2相等
ab3
5. SET中字符介绍
SETs are specified as strings of characters. Most represent themselves. Interpreted sequences are:
SETs是指的字符串,大部分场景是字符串本身。转移序列如下:
5.1 \NNN
\NNN character with octal value NNN (1 to 3 octal digits)
八进制表示字符比如 \141 -> a \143->c , 参照 ASCII 码表 - cppreference.com
renhl@verygood:~/usb$ echo "abc" | tr -d '\141\143' #删除abc中的a(\141)和c(\143)
b
5.2 \\
backslash 反斜杠
renhl@verygood:~/usb$ echo "a\b\c" #显示\
a\b\c
renhl@verygood:~/usb$ echo "a\b\c" | tr -d '\\' #删除字符串中的反斜杠\
abc
5.3 \a
audible BEL 叮咚的声音
renhl@verygood:~/usb$ echo -e "abc\a" #执行有叮咚的声音 \a
abc
renhl@verygood:~/usb$ echo -e "abc\a" | tr -d '\a' #删除叮咚的声音 \a
abc
5.4 \b
backspace 回格键 类似电脑键盘的 backspace按键
renhl@verygood:~/usb$ printf "abc\b" #退格将c字符删除了,echo 没有起作用,使用printf演示
abrenhl@verygood:~/usb$
renhl@verygood:~/usb$ printf "abc\b" | tr -d '\b' #删除退格键,所以c显示出来
abcrenhl@verygood:~/usb$
5.5 \f
form feed 换页符
renhl@verygood:~/usb$ echo -e "a\fc" #换页符
a
c
renhl@verygood:~/usb$ echo -e "a\fc" | tr -d '\f' #删除换页符
ac
5.6 \n
new line 新的行
renhl@verygood:~/usb$ echo -e "a\nc" #换行符
a
c
renhl@verygood:~/usb$ echo -e "a\nc" | tr -d '\n' #删除换行符,注意echo默认输出带一个换行符
acrenhl@verygood:~/usb$
5.7 \r
return 回到当前行首
renhl@verygood:~/usb$ echo -e "abc\r1" #回到当前行首,在输出1覆盖了之前a
1bc
renhl@verygood:~/usb$ echo -e "abc\r1" | tr -d '\r' #删除return字符
abc1
5.8 \t
horizontal tab 水平tab
renhl@verygood:~/usb$ echo -e "a\tb\tc" #水平tab
a b c
renhl@verygood:~/usb$ echo -e "a\tb\tc" | tr -d '\t' #删除水平tab
abc
5.9 \v
vertical tab 垂直tab
renhl@verygood:~/usb$ echo -e "a\vb\vc" #垂直tab, 表示行为和\f类似
a
b
c
renhl@verygood:~/usb$ echo -e "a\vb\vc" | tr -d '\v' #删除垂直tab
abc
5.10 CHAR1-CHAR2
all characters from CHAR1 to CHAR2 in ascending order
比如 ‘‘a-z’’ : 所有小写字母 “A-Z” : 所有大写字母 “0-9” : 0到9的字符
renhl@verygood:~/usb$ echo "123456789" | tr -d '4-8' #删除4到8的数字
1239
renhl@verygood:~/usb$ echo "abcdefghi" | tr -d 'c-h' #删除c到h的字母
abi
5.11 [CHAR*]
in SET2, copies of CHAR until length of SET1
当前SET2中字符数少于SET1中时,重复以上字符使得SET2中字符数与SET1的相等。
注意[CHAR*]可以位于字符串的任何位置。
renhl@verygood:~/usb$ echo "abc12345" | tr '12345' '[C*]AB' #123替换C;4替换A;5替换B
abcCCCAB
renhl@verygood:~/usb$ echo "abc12345" | tr '12345' 'A[C*]B' #1替换A;234替换C;5替换B
abcACCCB
renhl@verygood:~/usb$ echo "abc12345" | tr '12345' 'AB[C*]' #1替换A;2替换B;345替换C 是默认行为
abcABCCC
renhl@verygood:~/usb$ echo "abc12345" | tr '12345' 'ABC' #1替换A;2替换B;345替换C
abcABCCC
5.12 [CHAR*REPEAT]
REPEAT copies of CHAR, REPEAT octal if starting with 0
CHAR重复REPEAT次数
renhl@verygood:~/usb$ echo "abc12345" | tr '12345' '[A*2][B*2]C' #A重复2次 B重复2次
abcAABBC
5.13 其余特殊字符
特殊符号 | 代表意义 |
---|---|
[:alnum:] | all letters and digits 代表英文大小写字符及数字,亦即O-9,A-Z,a-z |
[:alpha:] | all letters 代表任何英文大小写字符,亦即A-Z,a-z |
[:blank:] | all horizontal whitespace 代表空白键与[Tab]按键两者 |
[:cntrl:] | all control characters 代表键盘上面的控制按键,亦即包括CR,LF,Tab,Del…等等 |
[:digit:] | all digits 代表数字而已,亦即0-9 |
[:graph:] | all printable characters, not including space 除了空白字符(空白键与[Tab]按键)外的其他所有按键 |
[:lower:] | all lower case letters 代表小写字符,亦即a-z |
[:print:] | all printable characters, including space 代表任何可以被打印出来的字符 |
[:punct:] | all punctuation characters 代表标点符号(punctuation symbol),亦即:"'? ! ;:# $… |
[:space:] | all horizontal or vertical whitespace 任何会产生空白的字符,包括空白键,[Tab],CR等等 |
[:upper:] | all upper case letters 代表大写字符,亦即A-Z |
[:xdigit:] | all hexadecimal digits 代表16进位的数字类型,因此包括:0-9,A-F, a-f的数字与字符 |
[=CHAR=] | all characters which are equivalent to CHAR |