tr命令

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值