UNIX Shell 編程(5)

來源:互聯網
上載者:User

UNIX Shell 編程(5)

 

過濾器tr用來轉換來自標準輸入的字元。格式:
tr from-chars to-chars
from-chars 和 to-chars 是一個或多個字元。例如:
[root@localhost programs]# cat intro
The Unix operating system was pioneered by Ken 
Thompson and Dennis Ritchie at Bell Laboratories 
in the late 1960s. One of the primary goals in 
the design of the Unix system was to create an 
environment that promoted efficient program 
developments.
[root@localhost programs]# tr e x < intro
Thx Unix opxrating systxm was pionxxrxd by Kxn 
Thompson and Dxnnis Ritchix at Bxll Laboratorixs 
in thx latx 1960s. Onx of thx primary goals in 
thx dxsign of thx Unix systxm was to crxatx an 
xnvironmxnt that promotxd xfficixnt program 
dxvxlopmxnts.

-s選項
用於壓縮to-chars中重複的字元。例如:
[root@localhost programs]# cat lotsapaces 
This is an example of a
file that contains a lot
of blank spaces
[root@localhost programs]# tr -s ' ' < lotsapaces 
This is an example of a
file that contains a lot
of blank spaces

-d選項
用於刪除輸入資料流中的字元,格式為:
tr -d from-chars
刪除所有空格
[root@localhost programs]# tr -d ' ' < lotsapaces 
Thisisanexampleofa
filethatcontainsalot
ofblankspaces
用sed命令也可實現:
[root@localhost programs]# sed 's/ //g' lotsapaces 
Thisisanexampleofa
filethatcontainsalot
ofblankspaces

tr命令綜合執行個體:
————————————————————————————————————
tr 'X' 'x' 把所有大些X轉換為小寫x
tr '()' '{}' 把所有小括弧換為大括弧
tr '[A-Z]' '[N-ZA-M]' 把A-M字母轉換為N-Z,把N-Z轉換為A-M
tr -d '/14' 刪除所有換頁字元
tr -d '[0-9]' 刪除所有數字  
————————————————————————————————————

grep命令可以從一個或多個檔案中搜尋特定的字串模式,格式為:
grep pattern files
例如,從檔案intro找出含Unix的行:
[root@localhost programs]# grep Unix intro
The Unix operating system was pioneered by Ken 
the design of the Unix system was to create an 

-v選項 顯示不包含的行
例如,顯示intro檔案中所有不包括Unix的行:
[root@localhost programs]# grep -v 'Unix' intro
Thompson and Dennis Ritchie at Bell Laboratories 
in the late 1960s. One of the primary goals in 
environment that promoted efficient program 
developments.

例如,顯示目前的目錄下所有檔案中包含Unix或unix的行:
[root@localhost programs]# grep '[Uu]nix' *
intro:The Unix operating system was pioneered by Ken 
intro:the design of the Unix system was to create an 

-l選項 只給出包含給定模式的檔案清單,而不給出檔案中的匹配行。
[root@localhost programs]# grep -l '[Uu]nix' *
intro

-n選項 檔案中符合指定模式的每一行前都加上該行在檔案中的相對行號。
[root@localhost programs]# grep -n 'Unix' *
intro:1:The Unix operating system was pioneered by Ken 
intro:4:the design of the Unix system was to create an 

sort命令:
[root@localhost programs]# cat names
Tony
Emanuel
Lucy
Ralph
Fred
[root@localhost programs]# sort names
Emanuel
Fred
Lucy
Ralph
Tony

-u選項 在輸出結果中去除重複的行

-r選項 顛倒排序次序
[root@localhost programs]# sort -r names
Tony
Ralph
Lucy
Fred
Emanuel

sort的輸出重新導向:可用-o選項,也可用>符號
[root@localhost programs]# sort -r names > sorted_name1
[root@localhost programs]# sort names -o sorted_name2

-n選項 按第一列進行排序
如:
[root@localhost programs]# cat data2
5 27
2 12
3 33
23 2
-5 11
15 6
14 -9
[root@localhost programs]# sort -n data2
-5 11
2 12
3 33
5 27
14 -9
15 6
23 2

[root@localhost programs]# uniq names
Tony
Emanuel
Lucy
Ralph
Fred
Tony
[root@localhost programs]# sort names | uniq
Emanuel
Fred
Lucy
Ralph
Tony

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.