基础系列(五):tr

因为tr的使用比较灵活,这里直接给出示例,具体的使用方法不予列出。

1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中

[root@centos7 tmp]# cat /etc/issue | tr [a-z] [A-Z] > /tmp/issue.out
[root@centos7 tmp]# cat /tmp/issue.out 
\S
KERNEL \R ON AN \M

或者

[root@centos7 tmp]# tr [a-z] [A-Z] < /etc/issue > /tmp/issue.out
[root@centos7 tmp]# cat /tmp/issue.out 
\S
KERNEL \R ON AN \M

2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中

[root@centos7 tmp]# who | tr [a-z] [A-Z] > /tmp/who.out
[root@centos7 tmp]# cat /tmp/who.out 
USER     :0           2016-07-21 14:55 (:0)
USER     PTS/0        2016-07-29 10:32 (:0)
ROOT     PTS/1        2016-07-31 09:25 (10.1.250.29)
ROOT     PTS/2        2016-07-30 11:07 (10.1.250.29)
ROOT     PTS/6        2016-07-30 10:59 (10.1.250.29)

3、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:

Hello, I am 用户名,the system version is here,pleasehelp me to check it ,thanks!

操作系统版本信息

[user@centos7 ~]$ vim information 
Hello,I am user,the system version is here,please help me to check it,thanks!
cat /etc/centos-release
[user@centos7 ~]$ mail -s help root < informatio

4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开

[root@centos7 tmp]# cd
[root@centos7 ~]# pwd
/root
[root@centos7 ~]# ls
anaconda-ks.cfg  f1  initial-setup-ks.cfg  new
[root@centos7 ~]# ls -1 | tr '\n' ' '
anaconda-ks.cfg f1 initial-setup-ks.cfg new

5、file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和

[root@centos7 ~]# echo "1 2 3 4 5 6 7 8 9 10" > file1 
[root@centos7 ~]# cat file1 | tr ' ' '+' | bc
55

或者

[root@centos7 ~]# echo $(seq 1 10) > /file1
[root@centos7 ~]# for I in $(cat file1); do sum=$[$sum+$I]; done; echo $sum
55

或者

[root@centos7 ~]# echo $[`echo "1 2 3 4 5 6 7 8 9 10" | tr ' ' '+'`]
55

6、删除Windows文本文件中的'^M'字符

[root@centos7 ~]# cat -A f1.txt 
a^M$
^M$
b[root@centos7 ~]# cat f1.txt | tr -d '\r'
a

b[root@centos7 ~]#

注:cat f1.txt | tr -d '\r'不可写成cat -A f1.txt | tr -d '\r

7、处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格

[root@centos7 ~]# echo "xt., 1 jr#trmn2 c*/fe3 uz4" | tr -d [:punct:][:alpha:]
 1 2 3 4

8、将PATH变量每个目录显示在独立的一行

[root@centos7 ~]# echo $PATH | tr ':' '\n' 
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin

9、删除指定文件的空行

[root@centos7 ~]# cat /etc/issue
\S
Kernel \r on an \m

[root@centos7 ~]# cat /etc/issue | tr -s '\n'
\S
Kernel \r on an \m
[root@centos7 ~]#

10、将文件中每个单词(字母)显示在独立的一行,并无空行

[root@centos7 ~]# cat /etc/issue | tr -s '\n' | tr '\n' ' ' | tr ' ' '\n'
\S
Kernel
\r
on
an
\m
[root@centos7 ~]#

或者

[root@centos7 ~]# cat /etc/issue | tr ' ' '\n' | tr -s '\n'
\S
Kernel
\r
on
an
\m
[root@centos7 ~]#

或者(标准答案)

[root@centos7 ~]# [root@centos7 ~]# cat /etc/issue | tr -c '[:alpha:]' '\n' | tr -s '\n'

S
Kernel
r
on
an
m

11、查询/etc/rc.d/init.d/function文件中,各个单词的使用频率,并从小到大排列

[root@centos6 ~]# cat /etc/rc.d/init.d/functions | tr -c '[:alpha:]' '\n' | tr -s '\n' | sort | uniq -c | sort -n

总结:tr命令处理的不是原文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值