zoukankan      html  css  js  c++  java
  • Linux常用基本命令:tr-替换或者删除字符

    tr命令

    作用:从标准输入中替换,缩减或者删除字符,并将结果输出到标准输出

    格式:tr [option] [set1] [set2]

    tr [选项] [字符1] [字符2]

    把y替换m, o替换e,并不仅仅是yo替换me

    ghostwu@dev:~/linux/tr$ cat ghostwu.txt 
    hello,my name is ghostwu,
    my qq is 359173352@qq.com
    my blog is http://www.cnblogs.com/ghostwu
    nice to meet you,
    feel free to contact me.
    ghostwu@dev:~/linux/tr$ tr 'me' 'yo' < ghostwu.txt 
    hollo,yy nayo is ghostwu,
    yy qq is 359173352@qq.coy
    yy blog is http://www.cnblogs.coy/ghostwu
    nico to yoot you,
    fool froo to contact yo.

    小写字母变成大写

    ghostwu@dev:~/linux/tr$ tr '[a-z]' '[A-Z]' < ghostwu.txt 
    HELLO,MY NAME IS GHOSTWU,
    MY QQ IS 359173352@QQ.COM
    MY BLOG IS HTTP://WWW.CNBLOGS.COM/GHOSTWU
    NICE TO MEET YOU,
    FEEL FREE TO CONTACT ME.

    -d: 删除.     删除数字

    ghostwu@dev:~/linux/tr$ tr -d '[0-9]' < ghostwu.txt 
    hello,my name is ghostwu,
    my qq is @qq.com
    my blog is http://www.cnblogs.com/ghostwu
    nice to meet you,
    feel free to contact me.

    删除a, b, c中出现的任意一个字母

    ghostwu@dev:~/linux/tr$ tr -d ['a-c'] < ghostwu.txt 
    hello,my nme is ghostwu,
    my qq is 359173352@qq.om
    my log is http://www.nlogs.om/ghostwu
    nie to meet you,
    feel free to ontt me.

    删除文件中的换行符

    ghostwu@dev:~/linux/tr$ tr -d '
    ' < ghostwu.txt 
    hello,my name is ghostwu,my qq is 359173352@qq.commy blog is http://www.cnblogs.com/ghostwunice to meet you,feel free to contact me.

    -s:保留连续字符的第一个,其他的删除。 压缩连续字符

    ghostwu@dev:~/linux/tr$ echo 'gggghhostwwwu' | tr -s ghostwu
    ghostwu

    -c:取反。 把所有的非数字 都变成 #

    ghostwu@dev:~/linux/tr$ tr '[0-9]' '#' < ghostwu.txt 
    hello,my name is ghostwu,
    my qq is #########@qq.com
    my blog is http://www.cnblogs.com/ghostwu
    nice to meet you,
    feel free to contact me.
    ghostwu@dev:~/linux/tr$ tr -c '[0-9]' '#' < ghostwu.txt 
    ###################################359173352#############################################################################################ghostwu@dev:~/linux/tr$
  • 相关阅读:
    LeetCode 32. 最长有效括号(Longest Valid Parentheses)
    LeetCode 141. 环形链表(Linked List Cycle)
    LeetCode 160. 相交链表(Intersection of Two Linked Lists)
    LeetCode 112. 路径总和(Path Sum)
    LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)
    LightGBM新特性总结
    sql service 事务与锁
    C#泛型实例详解
    C# 中的委托和事件(详解)
    C# DateTime日期格式化
  • 原文地址:https://www.cnblogs.com/ghostwu/p/9064961.html
Copyright © 2011-2022 走看看