- Linux管理员教程
- 首页
- CentOS概述
- 基本的CentOS Linux命令
- 文件/文件夹管理
- 用户管理
- 配额管理
- Systemd服务启动和停止
- 使用systemctl进行资源管理
- 使用cgroups进行资源管理
- 进程管理
- 防火墙设置
- 在CentOS Linux中配置PHP
- 在CentOS Linux中设置Python
- 在CentOS Linux中配置Ruby
- 为CentOS Linux设置Perl
- 安装和配置Open LDAP
- 创建SSL证书
- 安装Apache Web服务器CentOS 7
- 在CentOS 7上设置MySQL
- 设置Postfix MTA和IMAP/POP3
- 安装匿名FTP
- 远程管理
- CentOS中的流量监控
- 日志管理
- 备份和恢复
- 系统更新
- Shell脚本
- 包管理
- 卷管理
- Linux管理员实用资源
- Linux管理员 - 快速指南
- Linux管理员 - 实用资源
- Linux管理员 - 讨论
Linux管理员 - tr命令
以下是tr的语法。此命令用于转换或删除字符。
tr [OPTION] SET1 [SET2]
以下是tr常用的开关和字符类。
| 命令 | 功能 |
|---|---|
| -d | 删除 |
| -s | 将SET1中重复的文本在SET2中压缩为单个出现。 |
| [:alnum:] | 字母数字字符 |
| [:alpha:] | 所有字母 |
| [:digit:] | 所有数字 |
| [:blank:] | 所有水平空格 |
| [:space:] | 所有水平或垂直空格 |
| [:graph:] | 所有可打印字符,不包括空格 |
| [:print:] | 所有可打印字符,包括空格 |
| [:punct:] | 所有标点符号 |
| [:lower:] | 所有小写字母 |
| [:upper:] | 所有大写字母 |
tr通常用于转换或删除字符串中的字符。可以将tr视为sed的替换命令的更简单的替代方案。从stdin读取与从文件读取。
在考虑应该使用“使用sed”还是“使用tr”时,最好遵循保持简单的原则。如果tr中的操作很简单,则使用它。但是,一旦开始考虑递归使用tr,最好使用sed的替换命令。
通常,tr会将[SET1]中的字符替换为[SET2]中的字符,除非使用了-d开关。然后,将删除[SET1]中流中的字符。
在我们的names.txt文件中使用tr将所有小写字母转换为大写字母 −
[root@centosLocal Documents]# tr [:lower:] [:upper:] < names.txt TED:DANIEL:101 JENNY:COLON:608 DANA:MAXWELL:602 MARIAN:LITTLE:903 BOBBIE:CHAPMAN:403 NICOLAS:SINGLETON:203 DALE:BARTON:901 AARON:DENNIS:305 SANTOS:ANDREWS:504 JACQUELINE:NEAL:102 [root@centosLocal Documents]#
让我们将“:”字符转换回制表符 −
[root@centosLocal Documents]# tr [:] [\\t] < names.txt Ted Daniel 101 Jenny Colon 608 Dana Maxwell 602 Marian Little 903 Bobbie Chapman 403 Nicolas Singleton 203 Dale Barton 901 Aaron Dennis 305 Santos Andrews 504 Jacqueline Neal 102 [root@centosLocal Documents]#
如果想保存结果呢?使用重定向很容易。
[root@centosLocal Documents]# tr [:] [\\t] < names.txt >> tabbedNames.txt [root@centosLocal Documents]# cat tabbedNames.txt Ted Daniel 101 Jenny Colon 608 Dana Maxwell 602 Marian Little 903 Bobbie Chapman 403 Nicolas Singleton 203 [root@centosLocal Documents]#
让我们在格式不正确的文本上使用-s或压缩选项 −
[root@centosLocal Documents]# cat lines.txt line 1 line 2 line 3 line 4 line 5 [root@centosLocal Documents]# tr -s [:blank:] ' ' < lines.txt >> linesFormat.txt [root@centosLocal Documents]# cat linesFormat.txt line 1 line 2 line 3 line 4 line 5 [root@centosLocal Documents]#
basic_centos_linux_commands.htm
广告