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, --complementuse the complement of SET1
-d, --deletedelete characters in SET1, do not translate
-s, --squeeze-repeatsreplace each sequence of a repeated character
that is listed in the last specified SET,
with a single occurrence of that character
-t, --truncate-set1first truncate SET1 to length of SET2
--helpdisplay this help and exit
--versionoutput version information and exit
删除字符
root@ubuntu:/home/fl# echo "Hello 123 word" | tr -d "0-9"
Helloword
使用补集
如把标准输入里面的大写字母和换行符之外的字符删掉
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
【Linux--使用tr命令对字符串做处理】或
root@ubuntu:/home/fl# echo "aaBB" | tr "[:lower:]" "[:upper:]"
AABB
字符去重
root@ubuntu:/home/fl# echo "nihaooo" | tr -s "o"
nihao
推荐阅读
- Linux|109 个实用 shell 脚本
- linux笔记|linux 常用命令汇总(面向面试)
- Linux|Linux--网络基础
- linux|apt update和apt upgrade命令 - 有什么区别()
- linux|2022年云原生趋势
- Go|Docker后端部署详解(Go+Nginx)
- 开源生态|GPL、MIT、Apache...开发者如何选择开源协议(一文讲清根本区别)
- GitHub|7 款可替代 top 命令的工具