Linux管理員 - sed命令



sed是一個複雜的實用程式,需要掌握。此命令流式傳輸編輯器以過濾和轉換文字。已經編寫了整本書專門用於熟練地使用sed。因此請記住,本教程的目的是介紹sed的三個基本常用用法:

  • 字元替換
  • 列印操作
  • 刪除操作

sed的常用命令語法為:

sed [options] [file to operate on]

以下是需要記住的常用sed開關。

開關 動作
-i 就地編輯檔案而不是流,-i[SUFFIX]建立備份檔案
-e 新增要執行的指令碼命令
-n 靜默,抑制自動列印
-r ReGex,在指令碼中使用擴充套件正則表示式

-i將把更改應用於檔案,而不是像傳遞到sed一樣編輯檔案的流。

sed在與-e選項一起使用時,擴充套件了命令以對流處理多個操作。這可以代替遞迴地管道化sed來完成。

echo "Windows and IIS run the Internet" | sed -e 's/Windows/Linux/' -e 's/ and           
IIS//'  -e 's/run/runs/' 
Linux runs the Internet

sed-n選項會抑制預設列印到標準輸出。如我們所見,使用sed的列印命令,每一行都將被複制到標準輸出。

bash-3.2# sed 'p' ./lines.txt  
line1 
line1 
line2 
line2

這次,我們使用sed-n選項:

bash-3.2# sed -n 'p' ./lines.txt  
line1 
line2

sed會將內容流傳送到標準輸出。當新增'p'或列印命令時,會為每一行傳送一個單獨的流,導致所有行在標準輸出中被複制。

sed替換命令

此命令使用's'選項指定。我們已經多次看到sed與其替換命令一起使用。以下是一個簡單的示例:

[root@centosLocal Documents]# echo "developers eat veggies and fruit" | sed -e
's/veggies/pizza/' -e 's/fruit/coffee/' 
developers eat pizza and coffee 
[root@centosLocal Documents]#

讓我們在一個名為dev.txt的檔案上試試這個:

[root@centosLocal centos]# cat dev.txt  
Developers code all night and sleep all day.
[root@centosLocal centos]#

現在,讓我們更改檔案的內容,而不僅僅是傳送到sed的輸出流:

[root@centosLocal centos]# sed -ibak 's/sleep/code/' ./dev.txt  
[root@centosLocal centos]# ls dev* 
dev.txt  dev.txtbak 
[root@centosLocal centos]# cat dev* 
Developers code all night and code all day. 
Developers code all night and sleep all day. 
[root@centosLocal centos]#

注意 - 我們使用-i選項和一個唯一的字尾來建立備份檔案。

sed列印命令

此命令使用'p'命令指定。

讓我們使用我們的names.txt檔案,輸出為了簡潔起見已進行編輯:

[root@centosLocal Documents]# sed -n "p" ./names.txt  
Ted:Daniel:101 
Jenny:Colon:608 
Dana:Maxwell:602 
Marian:Little:903 
Bobbie:Chapman:403 
Nicolas:Singleton:203 
Dale:Barton:901 
Aaron:Dennis:305

sed允許使用“地址”更細緻地定義列印到標準輸出的內容:

[root@centosLocal Documents]# sed -n "1,10p" ./names.txt
Ted:Daniel:101 
Jenny:Colon:608 
Dana:Maxwell:602 
Marian:Little:903 
Bobbie:Chapman:403 
Nicolas:Singleton:203 
Dale:Barton:901 
Aaron:Dennis:305 
Santos:Andrews:504 
Jacqueline:Neal:102 
[root@centosLocal Documents]#

就像head一樣,我們列印了names.txt檔案的前10行。

如果我們只想打印出那些在9樓有辦公室的人呢?

[root@centosLocal Documents]# sed -n "/90/p" ./names.txt 
Marian:Little:903 
Dale:Barton:901 
Kellie:Curtis:903: 
Gina:Carr:902 
Antonia:Lucas:901 
[root@centosLocal Documents]#

很簡單。我們還可以打印出所有人,除了那些在9樓有辦公室的人:

[root@centosLocal Documents]# sed -n '/90/ !p' ./names.txt 
Ted:Daniel:101 
Jenny:Colon:608 
Dana:Maxwell:602 
Bobbie:Chapman:403 
Nicolas:Singleton:203 
Aaron:Dennis:305 
Santos:Andrews:504 
Jacqueline:Neal:102 
Billy:Crawford:301 
Rosa:Summers:405 
Matt:Davis:305 
Francisco:Gilbert:101
Sidney:Mac:100 
Heidi:Simmons:204 
Matt:Davis:205 
Cristina:Torres:206 
Sonya:Weaver:403 
Donald:Evans:403

在上面的程式碼中,我們用!否定了/和/之間的'p'列印操作。這與“d”或刪除命令類似。但是,否定在sed中的結果可能會有所不同。因此,作為一個通用的規則:p用於列印並否定您不想要的內容。

sed刪除命令

如前所述,刪除命令與sed列印命令相反。讓我們從我們的name.txt檔案開始:

[root@centosLocal Documents]# sed 'd' ./names.txt  
[root@centosLocal Documents]#

沒有列印任何內容。在上面的命令中,我們要求sed從流中的標準輸出中刪除每一行。現在,讓我們只打印前兩行並“刪除”其餘的流:

[root@centosLocal Documents]# sed '1,2 !d' ./names.txt  
Ted:Daniel:101 
Jenny:Colon:608
[root@centosLocal Documents]#

看到了吧?類似於'p'或列印命令。現在讓我們用刪除命令做一些有用的事情。假設我們要刪除檔案中所有空行:

[root@centosLocal Documents]# cat lines.txt  
line1 
line2 
line3 
line4 
line5 
[root@centosLocal Documents]#

從複製和貼上電子郵件或使用非標準換行符格式化時,收到包含雜亂文字的此類檔案並不罕見。我們無需在vim中互動式編輯檔案,而是可以使用sed為我們完成這項工作。

[root@centosLocal Documents]# sed -i '/^\s*$/ d' ./lines.txt
[root@centosLocal Documents]# cat ./lines.txt  
line1 
line2 
line3 
line4 
line5 
[root@centosLocal Documents]#

該檔案現在以易於閱讀的方式格式化。

注意 - 在對重要檔案進行更改時,請使用-i開關。強烈建議附加檔案備份字尾以保留檔案內容(sed可能會因最輕微的錯誤而進行一些極端的更改)。

basic_centos_linux_commands.htm
廣告