tr命令介绍以及使用
目录:
1.tr命令的介绍
2.tr命令格式
3tr命令使用项.
4.常见的tr命令的使用
tr命令介绍
从标准输入中翻译、压缩和/或删除字符,写入标准输出,说白了就转换和删除字符。
tr命令格式
tr 参数 test1 test2
tr命令的使用项
-c, -C, –complement 用集合1中的字符串替换,要求字符集为ASCII。 -d, –delete 删除集合1中的字符而不是转换 -s, –squeeze-repeats 删除所有重复出现字符序列,只保留第一个;即将重复出现字符串压缩为一个字符串。 -t, –truncate-set1 先删除第一字符集较第二字符集多出的字符 字符的范围 [2-7] 表示数字范围 [c-w] 表示字母范围 [a-zA-Z] [nihao] 匹配列表 [^nihao] 取反,会匹配到列表中所有字符以外的字符 [[:digit:]] 表示任意数字 [[:alpha:]] 表示任意字母 [[:alnum:]] 表示任意字母或数字 [[:upper:]] 表示大写字母 [[:lower:]] 表示小写字母 [[:blank:]] 水平空白字符 [[:space:]] 水平或垂直空白字符 [[:punct:]] 特殊字符
tr命令的使用
1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[[email protected](04:55:33) ~]$ cat /etc/issue CentOS release 6.9 (Final) Kernel \r on an \m [[email protected](04:55:42) ~]$ tr a-z A-Z < /etc/issue > /tmp/issue.out [[email protected](04:56:28) ~]$ cat /tmp/issue.out CENTOS RELEASE 6.9 (FINAL) KERNEL \R ON AN \M [[email protected](04:56:36) ~]$
2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
[[email protected](05:00:14) ~]$ w 17:00:18 up 8:09, 2 users, load average: 0.00, 0.00, 0.00 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT root pts/0 192.168.18.1 09:30 0.00s 0.65s 0.00s w root pts/1 192.168.18.1 14:50 2:09m 0.03s 0.03s -bash [[email protected](05:00:18) ~]$ w | tr a-z A-Z > /tmp/who.out [[email protected](05:00:50) ~]$ cat /tmp/who.out 17:00:50 UP 8:09, 2 USERS, LOAD AVERAGE: 0.00, 0.00, 0.00 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT ROOT PTS/0 192.168.18.1 09:30 0.00S 0.66S 0.00S W ROOT PTS/1 192.168.18.1 14:50 2:09M 0.03S 0.03S -BASH [[email protected](05:01:02) ~]$
3、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am 用户名,The system version is here,please help me to
check it ,thanks!
操作系统版本信息
[[email protected](05:44:15) mail]$ su - gentoo [[email protected] ~]$ [[email protected] ~]$ echo "Hello, I am `whoami`,The system version is here,please to check it ,thanks! \n`cat /etc/redhat-release`" |mail -s "help" root [[email protected] ~]$ su - Password: [[email protected](05:45:07) ~]$ mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 1 message 1 new >N 1 Gentoo Distribution Sat Sep 2 17:44 19/706 "help" & 1 Message 1: From [email protected] Sat Sep 2 17:44:47 2017 Return-Path: <[email protected]> X-Original-To: root Delivered-To: [email protected] Date: Sat, 02 Sep 2017 17:44:47 +0800 To: [email protected] Subject: help User-Agent: Heirloom mailx 12.4 7/29/08 Content-Type: text/plain; charset=us-ascii From: [email protected] (Gentoo Distribution) Status: R Hello, I am gentoo,The system version is here,please to check it ,thanks! CentOS release 6.9 (Final) &
4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开
[[email protected](05:48:28) ~]$ ls 1sss Downloads oldfile testdir aaa epel-release-latest-6.noarch.rpm Package test.gz anaconda-ks.cfg Finished Pictures Videos back2017-08-31 install.log Public Desktop install.log.syslog Running Documents Music Templates [[email protected](05:48:31) ~]$ ls -1 | tr ‘\n‘ ‘ ‘ 1sss aaa anaconda-ks.cfg back2017-08-31 Desktop Documents Downloads epel-release-latest-6.noarch.rpm Finished install.log install.log.syslog Music oldfile Package Pictures Public Running Templates testdir test.gz Videos [[email protected](05:48:35) ~]$
5、计算1+2+3+..+99+100的总和
[[email protected](05:51:39) ~]$ echo {1..100} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 [[email protected](05:51:50) ~]$ echo {1..100} | tr ‘ ‘ ‘+‘ |bc 5050 [[email protected](05:52:15) ~]$
6、删除Windows文本文件中的‘^M’字符
首先准备一个windows文本文件,内容自定义。
[[email protected](07:03:31) ~]$ cat -A abc.txt a^M$ b^M$ c^M$ d^M$ [[email protected](07:03:47) ~]$ tr -d ‘\r‘ < abc.txt > abcd.txt | cat -A abcd.txt a$ b$ c$ d$ [[email protected](07:08:44) ~]$
7、处理字符串“xt.,l 1 jr#!$mn 2 c*/fe 3 uz 4”,只保留其中的数字和空格
[[email protected](07:12:42) ~]$ echo ‘xt.,l 1 jr#!$mn ip:127.0.0.1 2 c*/fe 3 uz 4‘ | tr -dc [0-9][:blank:] 1 127001 2 3 4[[email protected](07:13:30) ~]$
8、将PATH变量每个目录显示在独立的一行
[[email protected](07:15:38) ~]$ echo $PATH /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [[email protected](07:15:42) ~]$ [[email protected](07:15:43) ~]$ echo $PATH | tr : ‘\n‘ /usr/lib64/qt-3.3/bin /usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin /root/bin [[email protected](07:15:45) ~]$
9、将指定文件中0-9分别替代成a-j
[[email protected](07:23:24) ~]$ cat abc.txt 0123456789 [[email protected](07:23:31) ~]$ cat abc.txt | tr [0-9] [a-j] abcdefghij [[email protected](07:23:35) ~]$
10、将文件中每个单词(由字母组成)显示在独立的一行,并无空行
[[email protected](07:26:25) ~]$ cat abc.txt Hello World! China! g o o d ! [[email protected](07:26:31) ~]$ cat abc.txt |tr ‘ ‘ ‘\n‘ | tr -s ‘\n‘ Hello World! China! g o o d ! [[email protected](07:27:32) ~]$
时间: 2024-10-07 09:59:40