Linux初體驗之練習篇(七)

Linux初體驗之練習篇(七)


  • tr

  • IO重定向

  • 神奇的自動掛載


tr

主要用途

轉換或刪除字符

tr - translate or delete characters
tr [OPTION]... SET1 [SET2]

tr命令是一個強大的字符轉換工具,可以對來自標準輸入的字符進行各種轉換,包括字符集對應轉換、刪除或都取字符補集、壓縮和格式調整。格式調整包括換行、回車、加入製表符等。

常用參數

  1. -c, -C: --complemen, 取字符集的補集

  2. -d: --delete, 刪除所有屬於第一個字符集的字符

  3. -s: --squeeze-repeats, 壓縮連續重複的字符爲單獨的一個字符

  4. -t: --truncate-set1, 將第一個字符集對應的字符轉換爲第二個字符集對應的字符

tr對字符的格式調整

\\     backslash
\a     audible BEL
\b     backspace
\f     form feed     # 換頁
\n     new line
\r     return
\t     horizontal tab
\v     vertical tab

tr可用的字符集:

使用格式:tr '[:lower:]' '[:upper:]'
[:alnum:]:字母和數字 
[:alpha:]:字母 
[:cntrl:]:控制(非打印)字符 
[:digit:]:數字 
[:graph:]:圖形字符 
[:lower:]:小寫字母 
[:print:]:可打印字符 
[:punct:]:標點符號 
[:space:]:空白字符 
[:upper:]:大寫字母 
[:xdigit:]:十六進制字符

使用示例見末尾的練習

IO重定向

我們都知道,所謂程序,可簡單表示爲:程序=指令+數據,而在Linux系統中,數據流可分爲三大類:

標準輸入:standard input ——0 默認接受來自鍵盤的輸入
標準輸出:standard output ——1 默認輸出到終端窗口
標準錯誤輸出:standard error ——2 默認輸出到終端窗口

所以,IO重定向就是改變IO輸入輸出的默認位置!而IO重定向的方式就要有 重定向符號與管道 兩種方式。

另外,在Linux中,每個文件都有一個文件描述符 fd (file descriptor), 而這些文件描述符fd都會關聯到一個設備上,Linux就是就是通過這些文件描述符來訪問文件的!

查看文件描述符fd:

[root@centos6 ~]# cd /proc

wKiom1ef7Veju5pKAADNyjoA4CE884.png

1. 輸出重定向操作符

格式:命令  操作符   文件名

>   把STDOUT重定向到文件,文件內容會被覆蓋
2>  把STDERR重定向到文件
&>  把所有輸出重定向到文件

>>  原有內容基礎上,追加內容
>|  強制覆蓋2>: 覆蓋重定向錯誤輸出數據流
2>>: 追加重定向錯誤輸出數據流

合併標準輸出和錯誤輸出爲同一個數據流進行重定向:
&>:覆蓋重定向
&>>:追加重定向
COMMAND > /path/to/file.out 2>&1 (順序很重要)
COMMAND >> /path/to/file.out 2>>&1
():多條命令輸出重定向
(cal2007;cal2008) > all.txt

標準輸出和錯誤輸出各自定向至不同位置:
COMMAND > /path/to/file.out 2> /path/to/error.out

注:爲避免文件被誤覆蓋:有以下兩個功能選項

# set -C: 禁止覆蓋,但可追加
# set +C: 允許覆蓋

2. 輸入重定向操作符

格式:命令  操作符   文件名
      命令  操作符   終止詞
      
<   重定向標準輸入
<<終止詞   多行輸入重定向

3. 管道

在linux系統中,管道的主要功能是將其他程序的輸出結果直接導出到另一程序,
來做輸入數據,即將前一程序的輸出作爲後一個程序的輸入,符號爲"|"。
管道的語法格式爲:
    COMMAND1 | COMMAND2 | COMMAND3 ...

將標準錯誤輸出一起輸入管道,命令格式爲:
    COMMAND1 |& COMMAND2 |& COMMAND3 ...

4. tee: 重定向到多個目標

tee

主要用途

重定向數據到文件且保存一個副本作爲後續命令的輸入

tee - read from standard input and write to standard output and files
tee [OPTION]... [FILE]...

tee命令作爲重定向數據的一個得力工具,其最大的特點是能夠“分流”數據,一邊可以重定向數據到目標文件,一邊可以將重定向的數據保存一個副本作爲後續命令的標準輸入。一句話,就是把數據重定向到目標文件並輸出到屏幕。

常用參數

  1. -a: --append,重定向時追加而不覆蓋

  2. -i: --ignore-interrupts, 忽略中斷信號

使用示例

[root@centos6 ~]# ls | tee /testdir/ls.log
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
Music
Pictures
Public
Templates
Videos
[root@centos6 ~]# cat /testdir/ls.log
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
Music
Pictures
Public
Templates
Videos
[root@centos6 ~]#
[root@centos6 ~]# ls | tee out.txt | cat -n
     1	anaconda-ks.cfg     2	Desktop     3	Documents     4	Downloads     5	install.log
     6	install.log.syslog     7	Music     8	Pictures     9	Public    
     10	Templates    11	Videos
[root@centos6 ~]# cat out.txt
anaconda-ks.cfg
Desktop
Documents
Downloads
install.log
install.log.syslog
Music
Pictures
Public
Templates
Videos
[root@centos6 ~]#

神奇的自動掛載

在centos 6下有個神奇的自動掛載文件:misc

[root@centos6 ~]# rpm -i /misc/cd/Packages/SOFTNAME

雖然df命令查不出其痕跡:

[root@centos6 ~]# dfFilesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2      100660656 4614984  90925672   5% /
tmpfs             953648     224    953424   1% /dev/shm
/dev/sda1         194241   39141    144860  22% /boot
/dev/sda3       20027260  333820  18669440   2% /testdir
/dev/sr0         3824484 3824484         0 100% /media/CentOS_6.8_Final

神奇的是可直接使用 # rpm -i /misc/cd/Packages/SOFTNAME 直接掛載。

練習一

1、使用別名命令,每日將/etc/目錄下所有文件,備份到/testdir/下獨立的新目錄下,並要求新目錄格式爲backupYYYY-mm-dd ,備份過程可見。

2、先創建/testdir/rootdir目錄,再複製/root所有下文件到該目錄內,並要求保留原有權限。

1、
[root@centos6 testdir]# alias backup='cp -rv /etc/ /testdir/backup`date +%F`'

wKiom1ef7dPja2uWAABPafJPIm0481.png

2、
[root@centos6 ~]# cp -r --preserv=mode /root/ /testdir/rootdir

wKioL1ef7feiWIZAAABNIOQX7IE507.png

練習二

1、將/etc/issue文件中的內容轉換爲大寫後保存至/tmp/issue.out文件中

[root@centos6 ~]# tr 'a-z' 'A-Z' < /etc/issue >/tmp/issue.out

wKioL1ef7mGzHBU_AABwwPftLRs364.png

2、將當前系統登錄用戶的信息轉換爲大寫後保存至/tmp/who.out文件中

[root@centos6 ~]# w | tr 'a-z' 'A-Z' > /tmp/who.out

wKiom1ef7uWhLlidAAByfWiSlWo789.png

3、一個linux用戶給root發郵件,要求郵件標題爲”help”,郵件正文如下: Hello, I am 用戶名,the system version is here,pleasehelp me to check it ,thanks! 操作系統版本信息

[liansir@centos6 ~]$ echo -e "Hello, I am $(whoami), the system version is here, please help me to check it, 
thanks! \n$(lsb_release -a)" | mail -s help root
[liansir@centos6 ~]$

wKiom1ef75KyJgKTAAC26for-lo105.png

4、將/root/下文件列表,顯示成一行,並文件名之間用空格隔開。

[root@centos6 ~]# ls -x 
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
[root@centos6 ~]# ls --format=horizontal
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
或:
[root@centos6 ~]# 
[root@centos6 ~]# ls -a | tr '\n' ' '

wKiom1ef8GvC1xe8AABbcb9tMjI527.png


wKiom1ef847CK2_yAABnvZ9MZDs118.png

5、file1文件的內容爲:”1 2 3 4 5 6 7 8 9 10” 計算出所有數字的總和

[root@centos6 ~]# echo "1 2 3 4 5 6 7 8 9 10" | tr ' ' + | bc
55
[root@centos6 ~]# 
或
[root@centos6 ~]# echo $[`echo "1 2 3 4 5 6 7 8 9 10" | tr ' ' + `]
55
[root@centos6 ~]#
或
[root@centos6 ~]# echo "1 2 3 4 5 6 7 8 9 10" > file2
[root@centos6 ~]# tr ' ' + < file2 | bc
55
[root@centos6 ~]# 
或
[root@centos6 ~]# cat file3
1 2 3 4 5 6 7 8 9 10
# 以下兩種方法應該要優於上面的,假如一個文件中有1....10000的數的話。。。。
[root@centos6 ~]# tr " " "+" < file3 | bc   
55
[root@centos6 ~]# tr ' ' + < file3 | bc
55
[root@centos6 ~]#

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

[root@centos6 ~]# tr -d '\r' < win.txt > win2.txt

wKioL1ef8aXBPRluAAA-4Fbz4fM694.png

wKiom1ef8buh7Qo0AAA8deQ_SUQ983.png

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

[root@centos6 ~]# echo 'xt.,l 1 jr#!$mn2 c*/fe3 uz4' | tr -cd '[:digit:] \n'

wKioL1ef8d6i7xWqAAAhM8UDqJg373.png

8、將PATH變量每個目錄顯示在獨立的一行

[root@centos6 ~]# echo $PATH | tr ':' '\n'


wKioL1ef8fbRvSFFAAA8HMCMTz8076.png

9、刪除指定文件的空行

[root@centos6 ~]# cat win.txt |tr -d '\r' |tr -s '\n'

wKiom1ef8haiSpvHAABGKjKcR4k792.png

10、將文件中每個單詞(字母)顯示在獨立的一行,並無空行

[root@centos6 ~]# cat f1 | tr -cs '[:alpha:]' '\n'

wKiom1ef8jCT2qMVAABL5Nuenk0660.png


止戰

2016.8.1



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章