Linux--使用tr命令对字符串做处理

tr命令可以对来自标准输入的字符进行转换,压缩和删除处理,很强大

tr命令帮助信息的部分内容如下:

root@ubuntu:/home/fl# tr --help
Usage: tr [OPTION]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input,
writing to standard output.

  -c, -C, --complement    use the complement of SET1
  -d, --delete            delete characters in SET1, do not translate
  -s, --squeeze-repeats   replace each sequence of a repeated character
                            that is listed in the last specified SET,
                            with a single occurrence of that character
  -t, --truncate-set1     first truncate SET1 to length of SET2
      --help     display this help and exit
      --version  output version information and exit

删除字符

root@ubuntu:/home/fl# echo "Hello 123 word" | tr -d "0-9"
Hello  word

使用补集

如把标准输入里面的大写字母和换行符之外的字符删掉

HWroot@ubuntu:/home/fl# echo "Hello Word!" | tr -d -c "A-Z\n"
HW

字符串替换

root@ubuntu:/home/fl# echo "Hello Word!" | tr "!" "."
Hello Word.

root@ubuntu:/home/fl# echo "Hello Word!" | tr "Word" "A"
HellA AAAA!

大小写转换

root@ubuntu:/home/fl# echo "aaBB" | tr "a-z" "A-Z"
AABB

root@ubuntu:/home/fl# echo "aaBB" | tr "[:lower:]" "[:upper:]"
AABB

字符去重

root@ubuntu:/home/fl# echo "nihaooo" | tr -s "o"
nihao
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值