tr 命令
- tr - translate or delete characters
- tr - 从STDIN中替换,去重,删除字符打打印至STDOUT
- tr - 从STDIN读取数据
SYNOPSIS
tr [OPTION]... SET1 [SET2]
DESCRIPTION
-c, -C, --complement 取SET1的补集
-d, --delete 删除SET1中的字符
-t, --truncate-set1 只对位替换
-s, --squeeze-repeats 去重
示例
tr SET1 SET2 tr 将set1的字符替换为set2代表的字符;对位转换
[root@centos7 data]# tr abc 123
abcd
123d
[root@centos7 data]# tr [:lower:] [:upper:]
adausb
ADAUSB
[root@centos7 data]# tr 12345 xyz
12345678
xyzzz678
tr -t 只对位转换
[root@centos7 data]# tr -t 12345 xyz
212345678
yxyz45678
tr -d 删除SET1表示的字符
[root@centos7 data]# tr -d 123
a1b2c3
abc
删\n符号
[root@centos7 tmp]# echo -e "a\nb\nc" > f1
[root@centos7 tmp]# cat f1
a
b
c
[root@centos7 tmp]# xxd f1
0000000: 610a 620a 630a a.b.c.
[root@centos7 tmp]# tr -d "\n" < f1
abc[root@centos7 tmp]#
tr -dc [:digit:] 只显示数字,
但是回车不会显示,回车字符被tr删除啦~ 输入完ctrl+d
[root@centos7 tmp]# tr -dc 1234567890
12s1wdwf
dqtdfqwt527
121527[root@centos7 tmp]#
tr -s 去重;去重之后方便截取;去重之后还可替换
[root@centos7 tmp]# df -h | grep ^/dev|tr -s " "
/dev/sda2 100G 4.5G 96G 5% /
/dev/sda5 50G 45M 50G 1% /data
/dev/sda1 1014M 174M 841M 18% /boot
[root@centos7 tmp]# df -h | grep ^/dev|tr -s " " :
/dev/sda2:100G:4.5G:96G:5%:/
/dev/sda5:50G:45M:50G:1%:/data
/dev/sda1:1014M:174M:841M:18%:/boot