tr命令相关选项:
tr-转换或删除字符
常用选项:
-c:取字符集的补集
-d:删除匹配的指定字符集中的字符
-s:把连续重复的字符以单独一个字符表示
-t:先删除第一字符集较第二字符集多出的字符
\\:反斜杠
\a:响铃
\b:退格
\n:换行
\r:回车
[:alnum:] :所有的字母和数字
[:alpha:] :所有的字母
[:blank:] :所有呈水平排列的空白字符
[:cntrl:] :所有的控制字符
[:digit:]: 所有的数字
[:graph:] :所有的可打印字符,不包括空格
[:lower:] :所有的小写字母
[:print:] :所有的可打印字符,包括空格
[:punct:] :所有的标点字符
[:space:] :所有呈水平或垂直排列的空白字符
[:upper:] :所有的大写字母
tr相关练习题:
1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@localhost test]# cat /etc/issue CentOS release 6.8 (Final) Kernel \r on an \m Mage Education Learning Services http://www.magedu.com [root@localhost test]# cat /etc/issue|tr ‘a-z‘ ‘A-Z‘ > /tmp/issue.out [root@localhost test]# cat /tmp/issue.out CENTOS RELEASE 6.8 (FINAL) KERNEL \R ON AN \M MAGE EDUCATION LEARNING SERVICES HTTP://WWW.MAGEDU.COM [root@localhost test]#
2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
[root@localhost test]# who root pts/0 2016-07-31 17:08 (10.1.250.58) root pts/1 2016-07-31 17:54 (10.1.250.58) [root@localhost test]# who|tr ‘a-z‘ ‘A-Z‘ > /tmp/who.out [root@localhost test]# cat /tmp/who.out ROOT PTS/0 2016-07-31 17:08 (10.1.250.58) ROOT PTS/1 2016-07-31 17:54 (10.1.250.58) [root@localhost test]#
3、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am 用户名,the system version is here,pleasehelp me to check it ,thanks!
操作系统版本信息
user send message: [doodle@localhost ~]$ echo -e "Hello, I am `whoami`,the system version is `uname -a`,pleasehelp me to check it ,thanks\!"|mail -s ‘help‘ root [doodle@localhost ~]$ root receive: [root@localhost ~]# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 2 messages 1 new 1 doodle@localhost.loc Sun Jul 31 19:52 19/801 "help" >N 2 doodle@localhost.loc Sun Jul 31 20:50 18/790 "help" & Message 2: From doodle@localhost.localdomain Sun Jul 31 20:50:18 2016 Return-Path: <doodle@localhost.localdomain> X-Original-To: root Delivered-To: root@localhost.localdomain Date: Sun, 31 Jul 2016 20:50:18 +0800 To: root@localhost.localdomain Subject: help User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: doodle@localhost.localdomain Status: R Hello, I am doodle,the system version is Linux localhost.localdomain 2.6.32-642.el6.x86_64 #1 SMP Tue May 10 17:27:01 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux,pleasehelp me to check it ,thanks\!
4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开
[root@localhost ~]# ls /root/ | tr [[:space:]] ‘ ‘ a.log anaconda-ks.cfg createfile.sh Desktop Documents Downloads file1 filetype2.sh filetype.sh idsum.sh install.log install.log.syslog Music ntfs-3g_ntfsprogs-2016.2.22 ntfs-3g_ntfsprogs-2016.2.22.tgz Pictures Public root@10.1.252.100 sum2.sh sum.sh sysinfo.sh Templates test useradd.sh usertype.sh Videos VMwareTools-10.0.0-2977863.tar.gz vmware-tools-distrib [root@localhost ~]#
5、file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和
[root@localhost ~]# touch file1 [root@localhost ~]# echo "1 2 3 4 5 6 7 8 9 10" > file1 [root@localhost ~]# cat file1 | tr ‘ ‘ ‘+‘|bc 55 [root@localhost ~]#
6、删除Windows文本文件中的‘^M‘字符
[root@localhost Desktop]# cat -A test.txt abc^M$ def^M$ ecf^M$ [root@localhost Desktop]# cat -A test.txt |tr -d ‘^M‘ abc$ def$ ecf$ [root@localhost Desktop]#
7、处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
[doodle@localhost ~]$ echo "xt.,l 1 jr#rootmn2 c*/fe3 uz4" | tr ‘[[:punct:]]‘\ > ‘ ‘ | tr ‘[[:alpha:]]‘ ‘ ‘ 1 2 3 4 [doodle@localhost ~]$
8、将PATH变量每个目录显示在独立的一行
[doodle@localhost ~]$ echo $PATH |tr -d ‘:‘|tr ‘/‘ ‘\n‘ usr lib64 qt-3.3 bin usr local bin bin usr bin usr local sbin usr sbin sbin home doodle bin [doodle@localhost ~]$
9、删除指定文件的空行
[root@localhost ~]# cat test.sh qqqqqqqqqqq wwwwwwwwwwwwwwwww eeeeeeeeeeeee rrrrrrrrrrrrrr tttttttttttttt [root@localhost ~]# cat test.sh |tr -s ‘\n‘ qqqqqqqqqqq wwwwwwwwwwwwwwwww eeeeeeeeeeeee rrrrrrrrrrrrrr tttttttttttttt [root@localhost ~]#
10、将文件中每个单词(字母)显示在独立的一行,并无空行
[root@localhost ~]# cat test No matter how far you may fly, never forget where you come from. [root@localhost ~]# cat test | tr ‘[[:punct:]]‘ ‘\n‘|tr ‘[[:space:]]‘ ‘\n‘|tr -s ‘\n‘ No matter how far you may fly never forget where you come from [root@localhost ~]#
本文出自 “11880696” 博客,谢绝转载!
原文地址:http://11890696.blog.51cto.com/11880696/1832387