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 \MHTTP://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 < mail.txt
[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:]]" < grep.txt
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' < emout.txt
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 "\<[[:alpha:]]+\>" 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,但是數字也會出現,所以就沒有使用
本文出自 “學無止境” 博客,請務必保留此出處http://dashui.blog.51cto.com/11254923/1832487