标签:ctrl control blank white 输入 tgs bbb 控制 cal
转换和删除字符
支持标准输入
tr [OPTION]...SET1[SET2]
Translate, squeeze, and/or delete characters from standard input,writing 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 character
-t, --truncate-set1 first truncate SET1 to length of SET2##强制一一对应
--help display this help and exit
--version output version information and exit
定义第一个字符串和第二个字符串
[root@C8-1 ~]# tr ‘3579‘ ‘baip‘
1234567890
12b4a6i8p0
[root@C8-1 ~]# tr ‘3579‘ ‘bai‘
1234567890
12b4a6i8i0
[root@C8-1 ~]# tr ‘357‘ ‘baip‘
1234567890
12b4a6i890
增加选项 -t 强制一一对应
[root@C8-1 ~]# tr ‘135790‘ ‘bai‘
1234567890
b2a4i6i8ii
[root@C8-1 ~]# tr -t ‘135790‘ ‘bai‘
1234567890
b2a4i67890
像通配符的写法,但不是通配符,是tr命令中的写法。
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
[CHARREPEAT] 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 16进制
[=CHAR=] all characters which are equivalent to CHAR
[root@C8-1 ~]# tr ‘a-z’ ‘A-Z‘
dhaohwuifqhw]
GKDRKZXLITKZ]
13reqg35yhetdf
13UHTJ35ZKHWGI
[root@C8-1 ~]# tr ‘[:lower:]’ ‘[:upper:]‘
tr: misaligned [:upper:] and/or [:lower:] construct
[root@C8-1 ~]# tr ‘[:lower:]‘ ‘[:upper:]‘
qfoeihefu3310491fas
QFOEIHEFU3310491FAS
[root@C8-1 ~]# tr ‘[:upper:]‘ ‘[:lower:]‘
QEGVD$TGS%^$*^#@GSDW
qegvd$tgs%^$*^#@gsdw
[root@C8-1 ~]# echo {1..8} | tr ‘ ‘ ‘\n‘
1
2
3
4
5
6
7
8
tr -s
把连续的重复字符,表示为一个字符,压缩相同的
[root@C8-1 ~]# tr -s abc
aaabbbabababccccbcbcbc
ababababcbcbcbc
tr -d
只要发现有小写abc就给删掉
[root@C8-1 ~]# tr -d abc
123abc456def789hijk
123456def789hijk
123ABC456d
123ABC456d
tr -c
除了字符集以外的字符,ctrl+d结束
配合-d使用,从随机数中取数字和字母
[root@C8-1 ~]# cat -n /dev/urandom | tr -dc ‘[:alnum:]‘ | >un.text
[root@C8-1 ~]# echo {1..100} | tr ‘ ‘ + |bc
5050
[root@C8-1 ~]# echo {1..100} | tr ‘ ‘ - |bc
-5048
[root@C8-1 ~]# echo {1..100} | tr ‘ ‘ ‘*‘|bc
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
[root@C8-1 ~]# echo {1..100} | tr ‘ ‘ ‘/‘|bc
0
一个特殊的字符设备,里边放着随机数
[root@C8-1 ~]# ll /dev/urandom
crw-rw-rw-. 1 root root 1, 9 Jun 20 07:02 /dev/urandom
就相当于excel里边的查找和替换,挺好用的吧。
tr -dc ‘[:alnum:]‘ < /dev/urandom
标签:ctrl control blank white 输入 tgs bbb 控制 cal
原文地址:https://www.cnblogs.com/bpzblog/p/13172276.html