zoukankan      html  css  js  c++  java
  • 命令——tr


    文本处理工具命令——tr

    一帮助说明

    TR(1)                                       User Commands                                       TR(1)
    
    NAME
           tr - translate or delete characters
    
    SYNOPSIS
           tr [OPTION]... SET1 [SET2]
    
    DESCRIPTION
           Translate, squeeze, and/or delete characters from standard input, writing to standard output.

    二常用选项

    (一)删除字符或者分隔符

    -d,--delete delete characters in SET1,do not translate删除指定字符,不做替换

    -C,-C,--complement use the complement of SET1取删除指定字符的补集,相当r,reverse反转

    示例——删除空白符

    [root@centos73 ~]# df
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda2       52403200 1451152  50952048   3% /
    devtmpfs          487964       0    487964   0% /dev
    tmpfs             498988       0    498988   0% /dev/shm
    tmpfs             498988   14076    484912   3% /run
    tmpfs             498988       0    498988   0% /sys/fs/cgroup
    /dev/sr0         4364408 4364408         0 100% /mnt
    /dev/sda3       20961280   87452  20873828   1% /app
    /dev/sda1        1038336  126596    911740  13% /boot
    tmpfs              99800       0     99800   0% /run/user/0

     

    [root@centos73 ~]# df  |  tr   -d  ' '
    Filesystem1K-blocksUsedAvailableUse%Mountedon
    /dev/sda2524032001451152509520483%/
    devtmpfs48796404879640%/dev
    tmpfs49898804989880%/dev/shm
    tmpfs498988140764849123%/run
    tmpfs49898804989880%/sys/fs/cgroup
    /dev/sr0436440843644080100%/mnt
    /dev/sda32096128087452208738281%/app
    /dev/sda1103833612659691174013%/boot
    tmpfs998000998000%/run/user/0
    
    
    







    示例——使用设备生成随机数

    默认显示的很多随机数是乱码的,要对其进行过滤,取出前30个没有乱码的字符

    取出数字加字母就够用了

    [root@centos72 ~]# cat  /dev/urandom  |  tr  -cd  '[:alnum:]' |  head  -c30
    GcCS7V3zdbxf7ULACZxyileWOkVbb4[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    4HRYAPkEmR9IU5DmwUCfPH5yFchmWk[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    Xai5M56ExMu3lPTk3uItOrqOIyrHBi[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    EM19jKg8elb0XaBZ5fBt6HlzvADj6V[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    2nGHfnxibhXymvYY50933w9IKZY2a1[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    FtlUPlUBAJqGuCF8D8mBWUA1Qy4mIE[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    pP7Qq0iqqv3ualzDdf6JYcyA7nnL0X[root@centos72 ~]# cat  /dev/urandom  |  tr  -dc   '[:alnum:]' |  head  -c30
    mcrr0qTGkAgFQ4Qp8wQYEOXkth6KBh[root@centos72 ~]# 









    示例——取6个字符,包含大小写字母

    [root@centos71 ~]# tr -cd  'a-zA-Z0-9'< /dev/urandom  |  head  -c6
    5PWu0c[root@centos71 ~]# tr -cd  'a-zA-Z0-9'< /dev/urandom  |  head  -c6
    eMHkgL[root@centos71 ~]# tr -cd  'a-zA-Z0-9'< /dev/urandom  |  head  -c6
    RlO3EL[root@centos71 ~]# tr -cd  'a-zA-Z0-9'< /dev/urandom  |  head  -c6
    pGlDTU[root@centos71 ~]# tr -cd  'a-zA-Z0-9'< /dev/urandom  |  head  -c6
    0WI1oX[root@centos71 ~]# tr -cd  'a-zA-Z0-9'< /dev/urandom  |  head  -c6
    pwho9R[root@centos71 ~]# tr -cd  'a-zA-Z0-9'< /dev/urandom  |  head  -c6
    IRe8FV[root@centos71 ~]# 

    (二)-s保留连续字符的第1个字符,删除其他字符

     

    -s, --squeeze-repeats
                  replace  each input sequence of a repeated character that is listed in SET1 with a sin?
                  gle occurrence of that character

    [root@centos73 ~]# df |grep "/sd" |tr -s  " "
    /dev/sda2 52403200 1450956 50952244 3% /
    /dev/sda3 20961280 87448 20873832 1% /app
    /dev/sda1 1038336 126596 911740 13% /boot
    [root@centos73 ~]# df  |grep "/sd" 
    /dev/sda2       52403200 1450956  50952244   3% /
    /dev/sda3       20961280   87448  20873832   1% /app
    /dev/sda1        1038336  126596    911740  13% /boot





    [root@centos73 ~]# cat /etc/issue
    S
    Kernel 
     on an m
    
    
    [root@centos73 ~]# tr  'abcd'   'xyz'  <  /etc/issue
    S
    Kernel 
     on xn m
    
    [root@centos73 ~]# cat  /etc/issue
    S
    Kernel 
     on an m

    难点——tr的替换

    情形一:替换前字符长度等于替换后字符长度

    这种情况最简单,全部替换,并且一一对应

    [root@centos71 test2]# echo  abcd  >  tr3.txt
    [root@centos71 test2]# cat  tr3.txt
    abcd
    [root@centos71 test2]# tr  'abcd'  '1234'  <  tr3.txt
    1234

    情形二:替换前字符长度大于替换后字符长度

    前者多余的字符被后者最后一个字符替换

    [root@centos71 test2]# echo  abcd  >>  tr.txt
    [root@centos71 test2]# cat tr.txt
    abcd
    [root@centos71 test2]# tr  'abcd'  '123'
    ^C
    [root@centos71 test2]# tr  'abcd'  '123'  <  tr.txt 
    1233

    情形三:替换前字符长度小于替换后字符长度

    前者多余的字符保留

    [root@centos71 test2]# echo   abcd  >  tr2.txt
    [root@centos71 test2]# cat  tr2.txt
    abcd
    [root@centos71 test2]# tr  'abc'  '1234' <  tr2.txt
    123d

    情形四:替换前后字符出现重复情况

    替换为最后出现的字符

    [root@centos71 test2]# echo   abcba  >  tr4.txt
    [root@centos71 test2]# cat  tr4.txt
    abcba
    [root@centos71 test2]# tr  'abcba'  '123456'  <  tr4.txt
    54345

     








  • 相关阅读:
    在MYSQL中插入当前时间,就象SQLSERVER的GETDATE()一样,以及对mysql中的时间日期操作。
    mysql使用default来设置字段的默认值
    MySQL关于check约束无效的解决办法
    C# 加密算法汇总
    REST,Web 服务,REST-ful 服务
    C# HTML 生成 PDF
    SQL Server Connection Pooling (ADO.NET)
    ADO.NET Connection Pooling at a Glance
    正则表达式
    开发 ASP.NET vNext 初步总结(使用Visual Studio 2015 Preview )
  • 原文地址:https://www.cnblogs.com/wang618/p/11039070.html
Copyright © 2011-2022 走看看