zoukankan      html  css  js  c++  java
  • tr

    tr 功能说明:替换或删除字符。
     
    语法格式: tr  [option]   [set1]  [set2] 
     
    参数选项:
    -d  删除字符。
    -s  保留连续字符的第一个字符,删除其他字符。
    -c  使用第一个字符串(set1)的补集,取反。
     
     
    将a.log 文件中的"abc"替换成 "XYZ"。
    [root@testdb ~]# cat a.log
    I am very happy!
    abcdefgabc
    yes!
    [root@testdb ~]# tr 'abc' 'XYZ' < a.log > b.log
    [root@testdb ~]# cat b.log
    I Xm very hXppy!
    XYZdefgXYZ
    yes!
    
     
    将a.log 文件中大小写转换
    [root@testdb ~]# cat a.log
    I am very happy!
    abcdefgabc
    yes!
    [root@testdb ~]# tr '[a-z]'  '[A-Z]' < a.log > b.log
    [root@testdb ~]# cat b.log
    I AM VERY HAPPY!
    ABCDEFGABC
    YES!
    [root@testdb ~]# tr '[A-Z]'  '[a-z]' < a.log > b.log 
    [root@testdb ~]# cat b.log
    i am very happy!
    abcdefgabc
    yes!
    
     
    将数字0-9 替换成a-j
    [root@testdb ~]# cat a.log
    I am very happy!
    0123456789
    abcdefgabc12345689000
    yes!
    6666
    [root@testdb ~]# tr '[0-9]'  '[a-j]' < a.log > b.log 
    [root@testdb ~]# cat b.log 
    I am very happy!
    abcdefghij
    abcdefgabcbcdefgijaaa
    yes!
    gggg
    
     
    删除文件中出现的abc中的每个字符
    [root@testdb ~]# cat a.log
    I am very happy!
    0123456789
    abcdefgabc12345689000
    yes!
    6666
    [root@testdb ~]# tr -d 'abc' < a.log > b.log
    [root@testdb ~]# cat b.log
    I m very hppy!
    0123456789
    defg12345689000
    yes!
    6666
    
     
    删除 a.log 文件中出现的换行"
    "、制表"	"字符
    [root@testdb ~]# cat a.log
    I am very happy!
    0123456789
    abcdefgabc12345689000
    yes!
    6666
    [root@testdb ~]# tr -d '
    	' < a.log > b.log
    [root@testdb ~]# cat b.log
    I am very happy!0123456789abcdefgabc12345689000yes!6666
     
     
    删除连续字符
    [root@testdb ~]# echo  'aaaaabbbbbccccddddd111000222333' | tr -s abcd
    abcd111000222333
    [root@testdb ~]# echo  'aaaaabbbbbccccddddd111000222333' | tr -s abc
    abcddddd111000222333
    [root@testdb ~]# echo  'aaaaabbbbbccccddddd111000222333' | tr -s ab
    abccccddddd111000222333
    
     
    将 a.log 所有数字均替换为"*"
    [root@testdb ~]# cat a.log
    I am very happy!
    0123456789
    abcdefgabc12345689000
    yes!
    6666
    [root@testdb ~]# tr '0-9' '*' < a.log > b.log
    [root@testdb ~]# cat b.log
    I am very happy!
    **********
    abcdefgabc***********
    yes!
    ****
     
     
    使用参数-c,除了数字,其他的字符包括换行符都会替换为"*"
    [root@testdb ~]# cat a.log
    I am very happy!
    0123456789
    abcdefgabc12345689000
    yes!
    6666
    [root@testdb ~]# tr -c '0-9' '*' < a.log > b.log
    [root@testdb ~]# cat b.log
    *****************0123456789***********12345689000******6666*
  • 相关阅读:
    C# winform开发:Graphics、pictureBox同时画多个矩形
    C# “配置系统未能初始化” 异常解决
    Google Maps API V3 之 路线服务
    Google Maps API V3 之 图层
    Google Maps API V3 之绘图库 信息窗口
    Google 地图 API V3 之 叠加层
    驱动开发之libusb函数
    libusb的使用教程和例子
    libusb检测U盘插入
    使用libusb检测USB设备插拔状态
  • 原文地址:https://www.cnblogs.com/l10n/p/9416483.html
Copyright © 2011-2022 走看看