Sed教程(二):基本语法、循环、分支
sed使用簡單,我們可以提供sed命令直接在命令行或具有sed命令的文本文件的形式。本教程講解調用sed的例子,有這兩種方法:
Sed 命令行
以下是我們可以指定單引號在命令行sed命令的格式如下:
sed [-n] [-e] 'command(s)' files例子
考慮一下我們有一個文本文件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文件的完整顯示內容如下:
[jerry]$ sed '' 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的delete命令刪除某些行。讓我們刪除了第一,第二和第五行。在這里,要刪除給定的三行,我們已經指定了三個單獨的命令帶有-e選項。
[jerry]$ sed -e '1d' -e '2d' -e '5d' books.txt執行上面的代碼,會得到如下結果:
3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones, George R. R. Martin, 864sed腳本文件
下面是第二種形式,我們可以提供一個sed腳本文件sed命令:
sed [-n] -f scriptfile files首先,創建一個包含在一個單獨的行的文本commands.txt文件,每次一行為每個sed命令,如下圖所示:
1d 2d 5d現在,我們可以指示sed從文本文件中讀取指令和執行操作。這里,我們實現相同的結果,如圖在上述的例子。
[jerry]$ sed -f commands.txt books.txt執行上面的代碼,會得到如下結果:
3) The Alchemist, Paulo Coelho, 197 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 6) A Game of Thrones,George R. R. Martin, 864sed標準選項
sed支持可從命令行提供下列標準選擇。
?-n 選項
這是模式緩沖區的缺省打印選項。 GNU sed解釋器提供--quiet,--silent選項作為 -n選項的替代。
例如,下面 sed 命令不顯示任何輸出:
[jerry]$ sed -n '' quote.txt-e 選項
-e選項的編輯選項。通過使用此選項,可以指定多個命令。例如,下面 sed 命令打印每行兩次:
[jerry]$ sed -e '' -e 'p' quote.txt執行上面的代碼,會得到如下結果:
There is only one thing that makes a dream impossible to achieve: the fear of failure. There is only one thing that makes a dream impossible to achieve: the fear of failure. - Paulo Coelho, The Alchemist - Paulo Coelho, The Alchemist-f 選項
-f選項是用來提供包含sed命令的文件。例如,我們可以按如下方法通過文件指定一個打印命令:
[jerry]$ echo "p" > commands.txt [jerry]$ sed -n -f commands quote.txt執行上面的代碼,會得到如下結果:
There is only one thing that makes a dream impossible to achieve: the fear of failure. - Paulo Coelho, The Alchemist
像其他的編程語言,sed還提供了一個循環和分支工具來控制程序的執行流程。本教程將探討如何使用sed的循環和分支。
sed循環的工作原理類似于現代編程語言中的goto語句。 sed可以跳轉到標記標簽的行并繼續執行下面提供該標簽的剩余命令。
以下是對在sed定義一個標簽的語法。在這里,冒號后的名稱(:)暗示的標簽名稱。
:label :start :end :up要跳轉到一個特定的標簽,我們可以使用 b 命令后面跟標簽名稱。如果標簽的名稱省略,則 sed 跳轉到 sed 文件的末尾。
考慮一下我們有一個待處理文本文件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下面的例子是連接書名,并在一行用逗號分隔作者姓名。然后,它會搜索模式“Paulo”。如果能夠匹配,它打印一個連字符(- )在該行的前面,否則跳轉到打印行打印標簽。
[jerry]$ sed -n ' h;n;H;x s/\n/, / /Paulo/!b Print s/^/- / :Print 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讀入模式緩沖區第一行即書名和保持緩沖區保持為空。后執行-h命令模式緩沖區被復制到保留緩沖區。現在,這兩個緩沖區包含了本書即標題.?A Storm of Swords.?接下來n命令打印當前的模式緩沖區(在本例中沒有打印出來,因為-n選項),清除當前圖形緩沖區讀取輸入的下一行。現在模式緩沖區包含George R. R. Martin。
-
第三個命令跳到僅當模式不匹配,否則取代是由第四指令執行的標簽Print。
-
:Print?僅僅是一個標簽名,p是打印命令。
為了提高可讀性,每個sed命令被放置在一個單獨的行。然而,人們可以選擇將所有命令在一行中,如下所示:
[jerry]$ sed -n 'h;n;H;x;s/\n/, /;/Paulo/!b Print; s/^/- /; :Print;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
可以用t命令創建分支。 t 命令跳轉到標簽,只有在以前的替換命令是成功的。讓我們以前面的章節同樣的例子,但不是打印一個連字符(- ),現在我們印刷四連字符。下面的例子演示了 t 命令的用法。
[jerry]$ sed -n ' h;n;H;x s/\n/, / :Loop /Paulo/s/^/-/ /----/!t Loop 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我們已經討論了在前面的章節中的第一個命令。第三個命令定義一個標簽循環。第四命令上前置的連字符( - ),如果該行包含字符串“Paulo”和t命令重復這一過程,直到有四個連字符位于行的開頭。
為了提高可讀性,每個 sed 命令寫在一個單獨的行。否則,我們可以寫一行一個 sed 如下:
[jerry]$ sed -n 'h;n;H;x; s/\n/, /; :Loop;/Paulo/s/^/-/; /----/!t Loop; 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
from: http://www.yiibai.com/sed/sed_useful_recipes.html
總結
以上是生活随笔為你收集整理的Sed教程(二):基本语法、循环、分支的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sed教程(一):简介、环境设置、工作流
- 下一篇: Sed教程(三):模式缓冲区、模式范围