UNIX Shell 編程(4)

來源:互聯網
上載者:User

UNIX Shell 編程(4)

 

cut命令
可以從資料檔案或者命令的輸出中截取所需的資料域。
命令格式:cut -cchars file
chars表示要截取哪些文字,可以是數字。
file表示檔案,如果不指定file,cut從標準輸出讀入輸入,即可把cut命令作為管道的過濾器。
如:
[root@localhost misc]# who
root pts/1 2009-04-15 09:15 (10.3.34.117)
fedora tty7 2009-04-15 09:46 (:0)
[root@localhost misc]# who | cut -c1-8
root  
fedora  
說明:
cut -c1-8表示把輸入的每一行的第1到8個字元截取出來,並作為標準輸出。

cut命令的-d和-f選項
命令:cut -ddchar -ffields file
dchar是資料中分隔各欄位的分隔字元
fields表示要從檔案file中截取的欄位
如:
[root@localhost misc]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
fedora:x:500:500:Fedora:/home/fedora:/bin/bash
[root@localhost misc]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
fedora

cut的-f選項是對應定位字元。
如:
[root@localhost programs]# cat phonebook 
Alica Chebb 973-555-2015
Barbara Swingle 201-555-9257
Liz Stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony Iannino 973-555-1295
[root@localhost programs]# cut -f1 phonebook 
Alica Chebb
Barbara Swingle
Liz Stachiw
Susan Goldberg
Tony Iannino
[root@localhost programs]# cut -f2 phonebook 
973-555-2015
201-555-9257
212-555-2298
201-555-7776
973-555-1295

paste命令:與cut相反,它可以把多行合并在一起。
如:
[root@localhost programs]# cat names 
Tony
Emanuel
Lucy
Ralph
Fred
[root@localhost programs]# cat addresses 
55-23 Vine Street, Miami
39 University Place, New York
17E. 25th Street, New York
38 Chauncey St., Bensonhurst
17 E. 25th Street, New York
[root@localhost programs]# cat numbers 
(307)555-5356
(212)555-3456
(212)555-9959
(212)555-7741
(212)555-0040
[root@localhost programs]# paste names numbers addresses 
Tony (307)555-5356 55-23 Vine Street, Miami
Emanuel (212)555-3456 39 University Place, New York
Lucy (212)555-9959 17E. 25th Street, New York
Ralph (212)555-7741 38 Chauncey St., Bensonhurst
Fred (212)555-0040 17 E. 25th Street, New York

使用-d選項——則不使用定位字元分隔各行的多個字元,如下:
[root@localhost programs]# paste -d'+' names numbers addresses 
Tony+(307)555-5356+55-23 Vine Street, Miami
Emanuel+(212)555-3456+39 University Place, New York
Lucy+(212)555-9959+17E. 25th Street, New York
Ralph+(212)555-7741+38 Chauncey St., Bensonhurst
Fred+(212)555-0040+17 E. 25th Street, New York

-s選項,告訴paste把同一檔案中的行粘貼在一起。
如:
[root@localhost programs]# cat names
Tony
Emanuel
Lucy
Ralph
Fred
[root@localhost programs]# paste -s names
Tony Emanuel Lucy Ralph Fred

sed是用來編輯資料的程式,指流編輯器,sed不能用於互動。
格式:sed command file
command是作用於指定檔案file各行的跟ed同樣風格的命令。
如未指定檔案,則把標準輸入作為處理對象。
例子:
[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.
把Unix改為UNIX。
[root@localhost programs]# sed s/Unix/UNIX/ 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.

sed的-n選項
提取檔案的指定行。
例如:
[root@localhost programs]# sed -n '1,2p' intro
The Unix operating system was pioneered by Ken 
Thompson and Dennis Ritchie at Bell Laboratories 
只列印包含UNIX的行:
[root@localhost programs]# sed -n '/UNIX/p' temp
The UNIX operating system was pioneered by Ken 
the design of the UNIX system was to create an 

刪除行,例如:
[root@localhost programs]# sed '1,2d' intro
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.
刪除包含UNIX的行:
[root@localhost programs]# sed '/UNIX/d' temp
Thompson and Dennis Ritchie at Bell Laboratories 
in the late 1960s. One of the primary goals in 
environment that promoted efficient program 
developments.

sed命令匯總樣本:
——————————————————————————————————————
sed '5d'   刪除第5行
sed '/[Tt]est/d'   刪除包含test和Test的行
sed -n'20,25p' text   顯示text檔案的第20行到25行
sed '1,10s/unix/UNIX/g' intro   把intro檔案前10行的unix改為UNIX
sed '/jan/s/-1/-5/'   把所有包含jan的行中的第一個-1改為-5
sed 's/...//' data   刪除data檔案每一行的前3個字元
sed 's/...$//' data   刪除data檔案每一行的最後3個字元
sed -n 'l' text   顯示text檔案的所有行,並把所有不可列印字元顯示為/nn,定位字元顯示為/t
——————————————————————————————————————

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.