1、将/etc/issue文件中的内容转换为大写后保存至/tmp/issue.out文件中
[root@localhost ~]# cat /etc/issue | tr 'a-z' 'A-Z' > /tmp/issue.out
[root@localhost ~]# cat /tmp/issue.out
\S
KERNEL \R ON AN \M
HTTP://WWW.MAGEDU.COM
TTY IS \L
HOSTNAME IS \N
CURRENT TIME IS \T
2、将当前系统登录用户的信息转换为大写后保存至/tmp/who.out文件中
[root@localhost ~]# who | tr 'a-z' 'A-Z' > /tmp/who.out
[root@localhost ~]# cat /tmp/who.out
ROOT PTS/0 2016-07-29 09:28 (10.1.250.82)
ROOT PTS/1 2016-07-29 09:48 (10.1.250.82)
ROOT PTS/2 2016-07-29 10:52 (10.1.250.82)
3、一个linux用户给root发邮件,要求邮件标题为”help”,邮件正文如下:
Hello, I am 用户名,the system version is here,pleasehelp me to check it ,thanks!
操作系统版本信息
[xiaoshui@localhost ~]$ mail -s help root
[xiaoshui@localhost ~]$ cat mail.txt
Hello, I am xiaoshui,the system version is here,pleasehelp me to check it ,thanks!
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.2.1511 (Core)
Release: 7.2.1511
Codename: Core
刚开始以为可以使用` `进行命令引用,然后发现使用后发现还是原来的字符
4、将/root/下文件列表,显示成一行,并文件名之间用空格隔开
[root@localhost /]# ls / | xargs
bin boot dev etc f1 home lib lib64 media mnt opt proc root run sbin srv sys test testdir tmp usr var
5、file1文件的内容为:”1 2 3 4 5 6 7 8 9 10” 计算出所有数字的总和
[root@localhost ~]# cat file1 | tr ' ' '+' | bc
55
[root@localhost ~]# let s=0;for i in `cat file1` ;do s=$[$i+$s] ;done ; echo "sum=$s"
sum=55
网上还看到一种是用echo 1 2 3 4 5 6 7 8 9 10| xargs -n1 | echo $[ $(tr '\n' '+') 0 ]的,结果是正确的,最后一点tr '\n' '+'不明白什么意思
6、删除Windows文本文件中的'^M'字符
[root@localhost ~]# cat m.txt | tr -d '\r\n' > newm.txt
7、处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
[root@localhost ~]# tr -d "[[:punct:]]|[[:alpha:]]"
1 2 3 4
[root@localhost ~]# cat grep.txt
xt.,l 1 jr#!$mn2 c*/fe3 uz4
[root@localhost ~]# grep -E -o "[[:space:]]|[[:digit:]]" grep.txt
1
2
3
4
[root@localhost ~]# grep -E -o "[[:space:]]|[[:digit:]]" grep.txt | cat -A
$
1$
$
2$
$
3$
$
4$
使用grep但是会出现换行的情况,但是确实也按要求提取到了
8、将PATH变量每个目录显示在独立的一行
[root@localhost ~]# echo $PATH | tr ':' '\n'
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin
[root@localhost ~]# echo $PATH | xargs -d: -n1
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin
[root@localhost ~]# echo $PATH | grep -E -o "/?[[:alnum:]]*/?[[:alnum:]]*/[[:alnum:]]+\-?[[:alnum:]].?[[:alnum:]]+/[[:alnum:]]+:?" | tr -d ':'
/usr/lib64/qt-3.3/bin
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/root/bin
第三种同样使用正则表达式,最后去掉冒号还是用的tr命令,因为刚刚预习到正则,虽然麻烦,就当做练习吧
9、删除指定文件的空行
[root@localhost ~]# tr -s '\n'
hello nihao
hahah
how are you
how old are you
[root@localhost ~]# cat emout.txt
hello nihao
hahah
how are you
how old are you
[root@localhost ~]# sed -i '/^$/'d emout.txt
[root@localhost ~]# cat emout.txt
hello nihao
hahah
how are you
how old are you
tr -s 只是去掉空行显示出来,但是并没有真正的删除空行,使用sed却是可以
10、将文件中每个单词(字母)显示在独立的一行,并无空行
[root@localhost ~]# grep -E -o "\" al.txt
xiao
shui
nihao
ni
haha
ni
txt
j
moring
father
mother
[root@localhost ~]# cat al.txt
xiao shui nihao+++ni haha
ni.txt
j 3 2 num23mo
moring
father mother
刚开始也想用tr命令将空格转换为\n,但是数字也会出现,所以就没有使用