
- Sed 教程
- Sed - 首頁
- Sed - 概述
- Sed - 環境
- Sed - 工作流程
- Sed - 基本語法
- Sed - 迴圈
- Sed - 分支
- Sed - 模式緩衝區
- Sed - 模式範圍
- Sed - 基本命令
- Sed - 特殊字元
- Sed - 字串
- Sed - 模式管理
- Sed - 正則表示式
- Sed - 實用技巧
- Sed 有用資源
- Sed - 快速指南
- Sed - 有用資源
- Sed - 討論
流編輯器 - 基本命令
本章介紹幾個有用的 SED 命令。
刪除命令
SED 提供各種命令來操作文字。讓我們首先探索一下**刪除**命令。以下是執行刪除命令的方法
[address1[,address2]]d
**address1** 和 **address2** 分別是起始和結束地址,可以是行號或模式字串。這兩個地址都是可選引數。
顧名思義,刪除命令用於執行刪除操作,並且由於 SED 對行進行操作,因此可以說此命令用於刪除行。請注意,刪除命令僅從模式緩衝區中刪除行;該行不會發送到輸出流,並且原始檔案保持不變。以下示例說明了這一點。
[jerry]$ sed 'd' books.txt
但是輸出在哪裡?如果未提供行地址,則 SED 預設對每一行進行操作。因此,它會刪除模式緩衝區中的所有行。這就是為什麼該命令不會在標準輸出上列印任何內容的原因。
讓我們指示 SED 僅對某些行進行操作。以下示例僅刪除第 4 行。
[jerry]$ sed '4d' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
此外,SED 還接受使用逗號 (,) 的地址範圍。我們可以指示 SED 刪除 N1 到 N2 行。例如,以下示例刪除第 2 到第 4 行的所有行。
[jerry]$ sed '2, 4 d' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
SED 的地址範圍不僅限於數字。我們還可以將模式指定為地址。以下示例刪除作者 Paulo Coelho 的所有書籍。
[jerry]$ sed '/Paulo Coelho/d' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones, George R. R. Martin, 864
我們還可以使用文字模式指定地址範圍。以下示例刪除**Storm** 和**Fellowship** 模式之間的所有行。
[jerry]$ sed '/Storm/,/Fellowship/d' books.txt 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
除此之外,我們還可以將美元符號 ($) 、加號 (+) 和波浪號 (~) 運算子與 SED 一起使用。
寫入命令
我們在任何檔案上執行的一個重要操作是備份,即我們製作檔案的另一個副本。SED 提供**寫入**命令將模式緩衝區的內容儲存到檔案中。下面給出的是**寫入**命令的語法,它類似於**刪除**命令。
[address1[,address2]]w file
這裡,**address1** 和 **address2** 分別是起始和結束地址,可以是行號或模式字串。這兩個地址都是可選引數。
在上述語法中,**w** 指的是寫入命令,**file** 是您儲存內容的檔名。請注意**file** 引數。當提供檔名時,如果檔案不存在,則 SED 會動態建立該檔案,如果檔案已存在,則會覆蓋該檔案。
讓我們使用 SED 建立檔案的精確副本。請注意,**w** 和**file** 之間必須正好有一個空格。
[jerry]$ sed -n 'w books.bak' books.txt
我們建立了另一個名為**books.bak** 的檔案。現在驗證這兩個檔案是否具有相同的內容。
[jerry]$ diff books.txt books.bak [jerry]$ echo $?
執行上述程式碼後,您將獲得以下結果
0
您可以假設**cp** 命令執行完全相同的事情。是的!**cp** 命令執行相同的事情,但 SED 是一個成熟的實用程式。它允許建立僅包含原始檔中某些行的檔案。讓我們將偶數行儲存到另一個檔案中。
[jerry]$ sed -n '2~2 w junk.txt' books.txt [jerry]$ cat junk.txt
執行上述程式碼後,您將獲得以下結果
2) The Two Towers, J. R. R. Tolkien, 352 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones, George R. R. Martin, 864
您還可以將逗號 (,) 、美元符號 ($) 和加號 (+) 運算子與寫入命令一起使用。
除此之外,SED 還支援模式匹配與寫入命令。假設您想將各個作者的所有書籍儲存到單獨的檔案中。一種乏味且冗長的方式是手動執行此操作,而更智慧的方式是使用 SED。
[jerry]$ sed -n -e '/Martin/ w Martin.txt' -e '/Paulo/ w Paulo.txt' -e '/Tolkien/ w Tolkien.txt' books.txt
在上面的示例中,我們正在將每一行與模式進行匹配,並將匹配的行儲存到特定檔案中。這非常簡單。為了指定多個命令,我們使用了 SED 命令的**-e** 開關。現在讓我們看看每個檔案包含什麼內容
[jerry]$ cat Martin.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 6) A Game of Thrones, George R. R. Martin, 864
讓我們顯示檔案內容。
[jerry]$ cat Paulo.txt
執行上述程式碼後,您將獲得以下結果
3) The Alchemist, Paulo Coelho, 197 5) The Pilgrimage, Paulo Coelho, 288
讓我們顯示檔案內容。
[jerry]$ cat Tolkien.txt
執行上述程式碼後,您將獲得以下結果
2) The Two Towers, J. R. R. Tolkien, 352 4) The Fellowship of the Ring, J. R. R. Tolkien, 432
太棒了!我們得到了預期的結果。SED 確實是一個很棒的實用程式。
追加命令
任何文字編輯器最有用的操作之一是提供追加功能。SED 透過其追加命令支援此操作。下面給出的是追加命令的語法
[address]a\ Append text
讓我們在第 4 行之後追加一個新的書籍條目。以下示例顯示瞭如何執行此操作
[jerry]$ sed '4 a 7) Adultry, Paulo Coelho, 234' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 7) Adultry, Paulo Coelho, 234 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
在命令部分,**4** 表示行號,**a** 是追加命令,其餘部分是要追加的文字。
讓我們在檔案末尾插入一行文字。為此,請使用**$** 作為地址。以下示例說明了這一點
[jerry]$ sed '$ a 7) Adultry, Paulo Coelho, 234' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864 7) Adultry, Paulo Coelho, 234
除了行號之外,我們還可以使用文字模式指定地址。例如,以下示例在匹配字串**The Alchemist** 後追加文字。
[jerry]$ sed '/The Alchemist/ a 7) Adultry, Paulo Coelho, 234' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 7) Adultry, Paulo Coelho, 234 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
請注意,如果有多個匹配的模式,則文字將在每次匹配後追加。以下示例說明了這種情況。
[jerry]$ sed '/The/ a 7) Adultry, Paulo Coelho, 234' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 7) Adultry, Paulo Coelho, 234 3) The Alchemist, Paulo Coelho, 197 7) Adultry, Paulo Coelho, 234 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 7) Adultry, Paulo Coelho, 234 5) The Pilgrimage, Paulo Coelho, 288 7) Adultry, Paulo Coelho, 234 6) A Game of Thrones, George R. R. Martin, 864
更改命令
SED 提供**更改**或**替換**命令,用 c 表示。此命令有助於用新文字替換現有行。當提供行範圍時,所有行都作為一個組被單個文字行替換。下面給出的是更改命令的語法
[address1[,address2]]c\ Replace text
讓我們用其他一些文字替換第三行。
[jerry]$ sed '3 c 3) Adultry, Paulo Coelho, 324' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) Adultry, Paulo Coelho, 324 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
SED 還接受模式作為地址。在以下示例中,當模式匹配成功時替換一行。
[jerry]$ sed '/The Alchemist/ c 3) Adultry, Paulo Coelho, 324' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) Adultry, Paulo Coelho, 324 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
SED 還允許用一行替換多行。以下示例刪除第 4 到第 6 行,並用新文字替換它們。
[jerry]$ sed '4, 6 c 4) Adultry, Paulo Coelho, 324' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) Adultry, Paulo Coelho, 324
插入命令
插入命令的工作方式與追加命令非常相似。唯一的區別在於它在特定位置之前插入一行。下面給出的是插入命令的語法
[address]i\ Insert text
讓我們透過一些示例來了解插入命令。以下命令在第 4 行之前插入一個新條目。
[jerry]$ sed '4 i 7) Adultry, Paulo Coelho, 324' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 7) Adultry, Paulo Coelho, 324 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
在上面的示例中,**4** 是位置編號,**i** 表示插入命令,其餘部分是要插入的文字。
要在檔案開頭插入文字,請將行地址提供為**1**。以下命令說明了這一點
[jerry]$ sed '1 i 7) Adultry, Paulo Coelho, 324' books.txt
執行上述程式碼後,您將獲得以下結果
7) Adultry, Paulo Coelho, 324
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
此外,我們可以插入多行。以下命令在最後一行之前插入兩行。
[jerry]$ sed '$ i 7) Adultry, Paulo Coelho, 324
執行上述程式碼後,您將獲得以下結果
8) Eleven Minutes, Paulo Coelho, 304' books.txt 1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage,Paulo Coelho, 288 7) Adultry, Paulo Coelho, 324 8) Eleven Minutes, Paulo Coelho, 304 6) A Game of Thrones, George R. R. Martin, 864
請注意,要插入的條目在單獨的行上輸入,並以反斜槓 (\) 字元分隔。
轉換命令
SED 提供一個轉換字元的命令,它表示為**y**。它按位置轉換字元。下面給出的是轉換命令的語法
[address1[,address2]]y/list-1/list-2/
請注意,轉換基於字元在**list 1** 中的位置到字元在**list 2** 中相同位置的字元,並且這兩個列表都必須是顯式字元列表。不支援正則表示式和字元類。此外,**list 1** 和**list 2** 的大小必須相同。
以下示例將阿拉伯數字轉換為羅馬數字。
[jerry]$ echo "1 5 15 20" | sed 'y/151520/IVXVXX/'
執行上述程式碼後,您將獲得以下結果
I V IV XX
l 命令
您能否僅透過檢視它們來區分以空格分隔的單詞和僅以製表符分隔的單詞?當然不行。但是 SED 可以為您做到這一點。SED 使用**l** 命令顯示文字中的隱藏字元。例如,製表符用**\t** 表示,行尾用**$** 字元表示。下面給出的是**l** 命令的語法。
[address1[,address2]]l [address1[,address2]]l [len]
讓我們建立一個包含製表符的檔案進行演示。為簡單起見,我們將使用同一個檔案,只是用製表符替換空格。等等!但是如何做到這一點——透過在文字編輯器中開啟檔案並將每個空格替換為製表符?當然不行!我們可以利用 SED 命令來做到這一點。
[jerry]$ sed 's/ /\t/g' books.txt > junk.txt
現在讓我們使用**l** 命令顯示隱藏字元
[jerry]$ sed -n 'l' junk.txt
執行上述程式碼後,您將獲得以下結果
1)\tA\tStorm\tof\tSwords,George\tR.\tR.\tMartin,1216$ 2)\tThe\tTwo\tTowers,J.\tR.\tR.\tTolkien,352$ 3)\tThe\tAlchemist,Paulo\tCoelho,197$ 4)\tThe\tFellowship\tof\tthe\tRing,J.\tR.\tR.\tTolkien,432$ 5)\tThe\tPilgrimage,Paulo\tCoelho,288$ 6)\tA\tGame\tof\tThrones,George\tR.\tR.\tMartin\t,864$
與其他 SED 命令一樣,它也接受行號和模式作為地址。您可以自己嘗試一下。
讓我們仔細看看 SED 的另一個有趣功能。我們可以指示 SED 在特定數量的字元後執行換行。以下示例在 25 個字元後換行。
[jerry]$ sed -n 'l 25' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords,Geo\ rge R. R. Martin,1216$ 2) The Two Towers,J. R. \ R. Tolkien,352$ 3) The Alchemist,Paulo C\ oelho,197$ 4) The Fellowship of the\ Ring,J. R. R. Tolkien,4\ 32$ 5) The Pilgrimage,Paulo \ Coelho,288$ 6) A Game of Thrones,Geo\ rge R. R. Martin ,864$
請注意,在上面的示例中,換行限制是在 l 命令之後提供的。在這種情況下,它是 25 個字元。此選項是 GNU 特定的,可能不適用於其他版本的 SED。
換行限制為 0 表示除非有換行符,否則永遠不會換行。以下簡單命令說明了這一點。
[jerry]$ sed -n 'l 0' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords,George R. R. Martin,1216$ 2) The Two Towers,J. R. R. Tolkien,352$ 3) The Alchemist,Paulo Coelho,197$ 4) The Fellowship of the Ring,J. R. R. Tolkien,432$ 5) The Pilgrimage,Paulo Coelho,288$ 6) A Game of Thrones,George R. R. Martin,864$
退出命令
退出命令指示 SED 退出當前執行流程。它由**q** 命令表示。下面給出的是退出命令的語法
[address]q [address]q [value]
請注意,退出命令不接受地址範圍,它只支援單個地址。預設情況下,SED 遵循讀取、執行和重複工作流程;但是,當遇到退出命令時,它會簡單地停止當前執行。
讓我們列印檔案中的前 3 行。
[jerry]$ sed '3 q' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197
除了行號之外,我們還可以使用文字模式。以下命令在模式匹配成功時退出。
[jerry]$ sed '/The Alchemist/ q' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197
除此之外,SED 還可以接受一個**value**,它可以用作退出狀態。以下命令將其退出狀態顯示為 100。
[jerry]$ sed '/The Alchemist/ q 100' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197
現在讓我們驗證退出狀態。
[jerry]$ echo $?
執行上述程式碼後,您將獲得以下結果
100
讀取命令
我們可以指示 SED 讀取檔案的內容並在特定條件匹配時顯示它們。該命令由字母**r** 表示。下面給出的是讀取命令的語法。
[address]r file
請注意,**r** 命令和檔名之間必須正好有一個空格。
讓我們用一個簡單的例子來理解它。建立一個名為**junk.txt** 的示例檔案。
[jerry]$ echo "This is junk text." > junk.txt
以下命令指示 SED 讀取**junk.txt** 的內容並在第三行之後插入它們。
[jerry]$ sed '3 r junk.txt' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 This is junk text. 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
在上面的示例中,3 表示行地址,**r** 是命令名稱,**junk.txt** 是要顯示其內容的檔名。此外,GNU SED 還接受地址範圍。例如,以下命令在第 3、第 4 和第 5 行之後插入**junk.txt** 的內容。
[jerry]$ sed '3, 5 r junk.txt' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 This is junk text. 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 This is junk text. 5) The Pilgrimage, Paulo Coelho, 288 This is junk text. 6) A Game of Thrones, George R. R. Martin, 864
與其他 SED 命令一樣,read 命令也接受模式作為地址。例如,以下命令在模式匹配成功時插入junk.txt的內容。
[jerry]$ sed '/Paulo/ r junk.txt' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 197 This is junk text. 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 This is junk text. 6) A Game of Thrones, George R. R. Martin, 864
執行命令
我們可以使用execute命令從 SED 中執行外部命令。它由e表示。以下是 execute 命令的語法。
[address1[,address2]]e [command]
讓我們用一個簡單的例子來說明 execute 命令。以下 SED 命令在第三行之前執行 UNIX date 命令。
[jerry]$ sed '3 e date' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 Sun Sep 7 18:04:49 IST 2014 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
與其他命令一樣,它也接受模式作為地址。例如,以下示例在模式匹配成功時執行date命令。請注意,在每次模式匹配後,首先執行命令,然後顯示模式緩衝區的內容。
[jerry]$ sed '/Paulo/ e date' books.txt
執行上述程式碼後,您將獲得以下結果
1) A Storm of Swords, George R. R. Martin, 1216 2) The Two Towers, J. R. R. Tolkien, 352 Sun Sep 7 18:06:04 IST 2014 3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 Sun Sep 7 18:06:04 IST 2014 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864
如果您仔細觀察e命令的語法,您會注意到command是可選的。當在e之後沒有提供命令時,它將模式緩衝區的內容視為外部命令。為了說明這一點,讓我們建立一個包含一些簡單命令的commands.txt檔案。
[jerry]$ echo -e "date\ncal\nuname" > commands.txt [jerry]$ cat commands.txt
執行上述程式碼後,您將獲得以下結果
date cal uname
檔案中的命令不言自明。在e之後沒有command的情況下,SED會依次執行所有這些命令。以下簡單示例說明了這一點。
[jerry]$ sed 'e' commands.txt
執行上述程式碼後,您將獲得以下結果
Sun Sep 7 18:14:20 IST 2014 September 2014 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Linux
與其他 SED 命令一樣,execute 命令也接受所有有效的地址範圍。
雜項命令
預設情況下,SED 對單行進行操作,但它也可以對多行進行操作。多行命令用大寫字母表示。例如,與n命令不同,N命令不會清除和列印模式空間。相反,它在當前模式空間的末尾新增一個換行符(\n),並將輸入檔案中的下一行追加到當前模式空間,並透過執行其餘的 SED 命令繼續 SED 的標準流程。以下是N命令的語法。
[address1[,address2]]N
讓我們列印一個書籍標題及其各自作者的逗號分隔列表。以下示例說明了這一點。
[jerry]$ sed 'N; s/\n/, /g' books.txt
執行上述程式碼後,您將獲得以下結果
A Storm of Swords, George R. R. Martin The Two Towers, J. R. R. Tolkien The Alchemist, Paulo Coelho The Fellowship of the Ring, J. R. R. Tolkien The Pilgrimage, Paulo Coelho A Game of Thrones, George R. R. Martin
讓我們瞭解上面示例的工作原理。N命令將第一行,即A Storm of Swords讀入模式緩衝區,並在其後追加\n以及下一行。模式空間現在包含A Storm of Swords\nGeorge R. R. Martin。在下一步中,我們用逗號替換換行符。
與p命令類似,我們有一個P命令來列印由N命令建立的多行模式空間的第一部分(直到嵌入的換行符)。以下是P命令的語法,它與p命令類似。
[address1[,address2]]P
在前面的示例中,我們看到N命令建立了一個換行符分隔的書籍標題及其作者列表。讓我們僅列印它的第一部分,即僅列印書籍的標題。以下命令說明了這一點。
[jerry]$ sed -n 'N;P' books.txt
執行上述程式碼後,您將獲得以下結果
A Storm of Swords The Two Towers The Alchemist The Fellowship of the Ring The Pilgrimage A Game of Thrones
請注意,在沒有N的情況下,它的行為與p命令相同。以下簡單命令說明了這種情況。
[jerry]$ sed -n 'P' books.txt
執行上述程式碼後,您將獲得以下結果
A Storm of Swords George R. R. Martin The Two Towers J. R. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. R. Tolkien The Pilgrimage Paulo Coelho A Game of Thrones George R. R. Martin
除此之外,SED 還提供了一個v命令來檢查版本。如果提供的版本大於已安裝的 SED 版本,則命令執行將失敗。請注意,此選項是 GNU 特定的,可能不適用於其他 SED 變體。以下是v命令的語法。
[address1[,address2]]v [version]
首先,找出 SED 的當前版本。
[jerry]$ sed --version
執行上述程式碼後,您將獲得以下結果
sed (GNU sed) 4.2.2
在以下示例中,SED 版本大於版本 4.2.2,因此 SED 命令中止其執行。
[jerry]$ sed 'v 4.2.3' books.txt
執行上述程式碼後,您將獲得以下結果
sed: -e expression #1, char 7: expected newer version of sed
但是,如果提供的版本小於或等於版本 4.2.2,則命令按預期工作。
[jerry]$ sed 'v 4.2.2' books.txt
執行上述程式碼後,您將獲得以下結果
A Storm of Swords George R. R. Martin The Two Towers J. R. R. Tolkien The Alchemist Paulo Coelho The Fellowship of the Ring J. R. R. Tolkien The Pilgrimage Paulo Coelho A Game of Thrones George R. R. Martin