码迷,mamicode.com
首页 > 其他好文 > 详细

SHELL STUDY....tr的用法

时间:2014-08-14 23:56:16      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:linux   shell   tr   

一般我们使用tr来做字符串的替换,或者删除指定的字符串

tr的语法如下:

  tr [OPTION]... SET1 [SET2]

       -c, -C, --complement

              first complement SET1

将非SET1中的字符替换为SET2


[[email protected] ~]$ echo "lubinsu" | tr -c "l" "A"
lAAAAAAA

       -d, --delete
              delete characters in SET1, do not translate

删除包含了SET1中的字符:

[[email protected] ~]$ echo "lubinsu" | tr -d "u"
lbins


[[email protected] ~]$ echo "lu123b123ins41u" | tr -d "0-9"
lubinsu

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

去除重复字符,压缩为一个字符:

[[email protected] ~]$ echo "lubinsu" | tr -c "l\n" "A"
lAAAAAA
[[email protected] ~]$ echo "lubinsu" | tr -cs "l\n" "A"
lA

       -t, --truncate-set1

              first truncate SET1 to length of SET2

默认为-t:

[[email protected] ~]$ echo "lubinsu" | tr -t "lu" "abc"
abbinsb
[[email protected] ~]$ echo "lubinsu" | tr "lu" "abc"
abbinsb

其他示例:

大小写替换:

[[email protected] ~]$ echo "lubinsu" | tr "a-z" "A-Z"
LUBINSU
[[email protected] ~]$ echo "lubinsu" | tr [:lower:] [:upper:]
LUBINSU



SHELL STUDY....tr的用法,布布扣,bubuko.com

SHELL STUDY....tr的用法

标签:linux   shell   tr   

原文地址:http://blog.csdn.net/lubinsu/article/details/38565237

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!