tr命令-转换和删除字符
作者:互联网
tr
转换和删除字符
支持标准输入
格式
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
用tr命令实现从1到100的运算
[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
93326215443944152681699238856266700490715968264381621468592963895217\
59999322991560894146397615651828625369792082722375825118521091686400\
0000000000000000000000
[root@C8-1 ~]# echo {1..100} | tr ' ' '/'|bc
0
/dev/urandom 存放着随机字符
一个特殊的字符设备,里边放着随机数
[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
标签:字符,删除,upper,tr,C8,characters,root 来源: https://www.cnblogs.com/bpzblog/p/13172276.html