首页 > 代码库 > SHELL STUDY....tr的用法
SHELL STUDY....tr的用法
一般我们使用tr来做字符串的替换,或者删除指定的字符串
tr的语法如下:
tr [OPTION]... SET1 [SET2]
-c, -C, --complement
first complement SET1
将非SET1中的字符替换为SET2
[lubinsu@200-168-1-4 ~]$ echo "lubinsu" | tr -c "l" "A" lAAAAAAA
-d, --delete
delete characters in SET1, do not translate
删除包含了SET1中的字符:
[lubinsu@200-168-1-4 ~]$ echo "lubinsu" | tr -d "u" lbins
[lubinsu@200-168-1-4 ~]$ 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
去除重复字符,压缩为一个字符:
[lubinsu@200-168-1-4 ~]$ echo "lubinsu" | tr -c "l\n" "A" lAAAAAA [lubinsu@200-168-1-4 ~]$ echo "lubinsu" | tr -cs "l\n" "A" lA
-t, --truncate-set1 first truncate SET1 to length of SET2
默认为-t:
[lubinsu@200-168-1-4 ~]$ echo "lubinsu" | tr -t "lu" "abc" abbinsb [lubinsu@200-168-1-4 ~]$ echo "lubinsu" | tr "lu" "abc" abbinsb
其他示例:
大小写替换:
[lubinsu@200-168-1-4 ~]$ echo "lubinsu" | tr "a-z" "A-Z" LUBINSU [lubinsu@200-168-1-4 ~]$ echo "lubinsu" | tr [:lower:] [:upper:] LUBINSU
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。